From 7e058072049ba96f74160b3c0e8ad16610de2af7 Mon Sep 17 00:00:00 2001 From: Martin Durant Date: Fri, 22 Sep 2023 11:41:21 -0400 Subject: [PATCH] Allow size= in open() (#797) --- s3fs/core.py | 6 +++++- s3fs/tests/test_s3fs.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/s3fs/core.py b/s3fs/core.py index 3d1a61af..54c75201 100644 --- a/s3fs/core.py +++ b/s3fs/core.py @@ -604,6 +604,7 @@ def _open( fill_cache=None, cache_type=None, autocommit=True, + size=None, requester_pays=None, cache_options=None, **kwargs, @@ -680,6 +681,7 @@ def _open( autocommit=autocommit, requester_pays=requester_pays, cache_options=cache_options, + size=size, ) async def _lsdir( @@ -2056,6 +2058,7 @@ def __init__( cache_type="readahead", requester_pays=False, cache_options=None, + size=None, ): bucket, key, path_version_id = s3.split_path(path) if not key: @@ -2094,6 +2097,7 @@ def __init__( autocommit=autocommit, cache_type=cache_type, cache_options=cache_options, + size=size, ) self.s3 = self.fs # compatibility @@ -2133,7 +2137,7 @@ def __init__( # Reflect head self.s3_additional_kwargs.update(head) - if "r" in mode and "ETag" in self.details: + if "r" in mode and size is None and "ETag" in self.details: self.req_kw["IfMatch"] = self.details["ETag"] def _call_s3(self, method, *kwarglist, **kwargs): diff --git a/s3fs/tests/test_s3fs.py b/s3fs/tests/test_s3fs.py index 7fb400cb..8675589b 100644 --- a/s3fs/tests/test_s3fs.py +++ b/s3fs/tests/test_s3fs.py @@ -166,6 +166,18 @@ def test_simple(s3): assert out == data +def test_with_size(s3): + data = b"a" * (10 * 2**20) + + with s3.open(a, "wb") as f: + f.write(data) + + with s3.open(a, "rb", size=100) as f: + assert f.size == 100 + out = f.read() + assert len(out) == 100 + + @pytest.mark.parametrize("default_cache_type", ["none", "bytes", "mmap", "readahead"]) def test_default_cache_type(s3, default_cache_type): data = b"a" * (10 * 2**20)