Skip to content

Commit 26a00b5

Browse files
committed
feat: Add AWS profile support to fsspec
1 parent 6c6be10 commit 26a00b5

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

pyiceberg/io/fsspec.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
ADLS_TENANT_ID,
5252
ADLS_TOKEN,
5353
AWS_ACCESS_KEY_ID,
54+
AWS_PROFILE_NAME,
5455
AWS_REGION,
5556
AWS_SECRET_ACCESS_KEY,
5657
AWS_SESSION_TOKEN,
@@ -71,6 +72,7 @@
7172
S3_CONNECT_TIMEOUT,
7273
S3_ENDPOINT,
7374
S3_FORCE_VIRTUAL_ADDRESSING,
75+
S3_PROFILE_NAME,
7476
S3_PROXY_URI,
7577
S3_REGION,
7678
S3_REQUEST_TIMEOUT,
@@ -205,7 +207,13 @@ def _s3(properties: Properties) -> AbstractFileSystem:
205207
else:
206208
anon = False
207209

208-
fs = S3FileSystem(anon=anon, client_kwargs=client_kwargs, config_kwargs=config_kwargs)
210+
session = None
211+
if profile_name := get_first_property_value(properties, S3_PROFILE_NAME, AWS_PROFILE_NAME):
212+
from aiobotocore.session import AioSession
213+
214+
session = AioSession(profile=profile_name)
215+
216+
fs = S3FileSystem(anon=anon, client_kwargs=client_kwargs, config_kwargs=config_kwargs, session=session)
209217

210218
for event_name, event_function in register_events.items():
211219
fs.s3.meta.events.unregister(event_name, unique_id=1925)

tests/io/test_fsspec_profile.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
from unittest import mock
3+
import pytest
4+
from pyiceberg.io.fsspec import FsspecFileIO
5+
from pyiceberg.typedef import Properties
6+
7+
UNIFIED_AWS_SESSION_PROPERTIES = {
8+
"client.access-key-id": "client.access-key-id",
9+
"client.secret-access-key": "client.secret-access-key",
10+
"client.region": "client.region",
11+
"client.session-token": "client.session-token",
12+
}
13+
import uuid
14+
15+
def test_fsspec_s3_session_properties_with_profile() -> None:
16+
session_properties: Properties = {
17+
"s3.profile-name": "test-profile",
18+
"s3.endpoint": "http://localhost:9000",
19+
**UNIFIED_AWS_SESSION_PROPERTIES,
20+
}
21+
22+
with mock.patch("s3fs.S3FileSystem") as mock_s3fs, mock.patch("aiobotocore.session.AioSession") as mock_aio_session:
23+
s3_fileio = FsspecFileIO(properties=session_properties)
24+
filename = str(uuid.uuid4())
25+
26+
s3_fileio.new_input(location=f"s3://warehouse/{filename}")
27+
28+
mock_aio_session.assert_called_with(profile="test-profile")
29+
mock_s3fs.assert_called_with(
30+
anon=False,
31+
client_kwargs={
32+
"endpoint_url": "http://localhost:9000",
33+
"aws_access_key_id": "client.access-key-id",
34+
"aws_secret_access_key": "client.secret-access-key",
35+
"region_name": "client.region",
36+
"aws_session_token": "client.session-token",
37+
},
38+
config_kwargs={},
39+
session=mock_aio_session(),
40+
)
41+
42+
def test_fsspec_s3_session_properties_with_client_profile() -> None:
43+
session_properties: Properties = {
44+
"client.profile-name": "test-profile",
45+
"s3.endpoint": "http://localhost:9000",
46+
**UNIFIED_AWS_SESSION_PROPERTIES,
47+
}
48+
49+
with mock.patch("s3fs.S3FileSystem") as mock_s3fs, mock.patch("aiobotocore.session.AioSession") as mock_aio_session:
50+
s3_fileio = FsspecFileIO(properties=session_properties)
51+
filename = str(uuid.uuid4())
52+
53+
s3_fileio.new_input(location=f"s3://warehouse/{filename}")
54+
55+
mock_aio_session.assert_called_with(profile="test-profile")
56+
mock_s3fs.assert_called_with(
57+
anon=False,
58+
client_kwargs={
59+
"endpoint_url": "http://localhost:9000",
60+
"aws_access_key_id": "client.access-key-id",
61+
"aws_secret_access_key": "client.secret-access-key",
62+
"region_name": "client.region",
63+
"aws_session_token": "client.session-token",
64+
},
65+
config_kwargs={},
66+
session=mock_aio_session(),
67+
)

0 commit comments

Comments
 (0)