Skip to content

Commit b4d3fa8

Browse files
create new session if refresh is True (#939)
--------- Co-authored-by: Martin Durant <martin.durant@alumni.utoronto.ca>
1 parent 744d9c6 commit b4d3fa8

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

s3fs/core.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,30 @@ def _prepare_config_kwargs(self):
480480

481481
async def set_session(self, refresh=False, kwargs={}):
482482
"""Establish S3 connection object.
483+
484+
This async method is called by any operation on an ``S3FileSystem`` instance.
485+
The ``refresh=True`` argument is useful if new credentials have been created
486+
and the instance needs to be reestablished. ``connect`` is a blocking
487+
version of ``set_session``.
488+
489+
Parameters
490+
----------
491+
refresh : bool (False)
492+
If True, create a new session even if one already exists.
493+
kwargs : dict
494+
Currently unused.
495+
483496
Returns
484497
-------
485498
Session to be closed later with await .close()
499+
500+
Examples
501+
--------
502+
>>> s3 = S3FileSystem(profile="<profile name>") # doctest: +SKIP
503+
# use in an async coroutine to assign the client object to a local variable
504+
>>> await s3.set_session() # doctest: +SKIP
505+
# blocking version of set_session
506+
>>> s3.connect(refresh=True) # doctest: +SKIP
486507
"""
487508
if self._s3 is not None and not refresh:
488509
return self._s3
@@ -522,7 +543,7 @@ async def set_session(self, refresh=False, kwargs={}):
522543
config_kwargs["signature_version"] = UNSIGNED
523544

524545
conf = AioConfig(**config_kwargs)
525-
if self.session is None:
546+
if self.session is None or refresh:
526547
self.session = aiobotocore.session.AioSession(**self.kwargs)
527548

528549
for parameters in (config_kwargs, self.kwargs, init_kwargs, client_kwargs):

s3fs/tests/test_s3fs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,11 @@ def test_user_session_is_preserved():
247247

248248

249249
def test_idempotent_connect(s3):
250-
first = s3.s3
251-
assert s3.connect(refresh=True) is not first
250+
stale_s3 = s3.s3
251+
stale_session = s3.session
252+
s3.connect(refresh=True)
253+
assert stale_s3 is not s3.s3
254+
assert stale_session is not s3.session
252255

253256

254257
def test_multiple_objects(s3):

0 commit comments

Comments
 (0)