Skip to content

Commit 327e3fc

Browse files
authored
Merge pull request #386 from martindurant/version_caching
Fixed cached directory info when versioned
2 parents 86e8dbd + 9b44f0d commit 327e3fc

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

s3fs/core.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,26 +1365,24 @@ def __init__(self, s3, path, mode='rb', block_size=5 * 2 ** 20, acl="",
13651365
self.fill_cache = fill_cache
13661366
self.s3_additional_kwargs = s3_additional_kwargs or {}
13671367
self.req_kw = {'RequestPayer': 'requester'} if requester_pays else {}
1368-
super().__init__(s3, path, mode, block_size, autocommit=autocommit,
1369-
cache_type=cache_type)
1370-
self.s3 = self.fs # compatibility
1371-
if self.writable():
1368+
if 'r' not in mode:
13721369
if block_size < 5 * 2 ** 20:
13731370
raise ValueError('Block size must be >=5MB')
13741371
else:
1375-
if version_id and self.fs.version_aware:
1372+
if version_id and s3.version_aware:
13761373
self.version_id = version_id
1377-
self.details = self.fs.info(self.path, version_id=version_id)
1374+
self.details = s3.info(path, version_id=version_id)
13781375
self.size = self.details['size']
1379-
elif self.fs.version_aware:
1380-
self.version_id = self.details.get('VersionId')
1376+
elif s3.version_aware:
13811377
# In this case we have not managed to get the VersionId out of details and
13821378
# we should invalidate the cache and perform a full head_object since it
13831379
# has likely been partially populated by ls.
1384-
if self.version_id is None:
1385-
self.fs.invalidate_cache(self.path)
1386-
self.details = self.fs.info(self.path)
1387-
self.version_id = self.details.get('VersionId')
1380+
s3.invalidate_cache(path)
1381+
self.details = s3.info(path)
1382+
self.version_id = self.details.get('VersionId')
1383+
super().__init__(s3, path, mode, block_size, autocommit=autocommit,
1384+
cache_type=cache_type)
1385+
self.s3 = self.fs # compatibility
13881386

13891387
# when not using autocommit we want to have transactional state to manage
13901388
self.append_block = False

s3fs/tests/test_s3fs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,3 +1691,25 @@ def test_shallow_find(s3):
16911691

16921692
assert s3.ls(test_bucket_name) == s3.find(test_bucket_name, maxdepth=1, withdirs=True)
16931693
assert s3.ls(test_bucket_name) == s3.glob(test_bucket_name + "/*")
1694+
1695+
1696+
def test_version_sizes(s3):
1697+
# protect against caching of incorrect version details
1698+
s3 = S3FileSystem(anon=False, version_aware=True,
1699+
client_kwargs={'endpoint_url': endpoint_uri})
1700+
import gzip
1701+
path = f"s3://{versioned_bucket_name}/test.txt.gz"
1702+
versions = [
1703+
s3.pipe_file(path, gzip.compress(text))
1704+
for text in (
1705+
b'good morning!',
1706+
b'hello!',
1707+
b'hi!',
1708+
b'hello!',
1709+
)
1710+
]
1711+
for version in versions:
1712+
version_id = version['VersionId']
1713+
with s3.open(path, version_id=version_id) as f:
1714+
with gzip.open(f) as zfp:
1715+
zfp.read()

0 commit comments

Comments
 (0)