Skip to content

Commit

Permalink
Let async file know its size after first read (#745)
Browse files Browse the repository at this point in the history
* Let async file know its size after first read

* initial size to None
  • Loading branch information
martindurant authored Jun 5, 2023
1 parent 61f96e1 commit 22f8ea1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions s3fs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2296,11 +2296,13 @@ def __init__(self, fs, path, mode):
self.mode = mode
self.r = None
self.loc = 0
self.size = None

async def read(self, length=-1):
if self.r is None:
bucket, key, gen = self.fs.split_path(self.path)
r = await self.fs._call_s3("get_object", Bucket=bucket, Key=key)
self.size = int(r["ResponseMetadata"]["HTTPHeaders"]["content-length"])
self.r = r["Body"]
out = await self.r.read(length)
self.loc += len(out)
Expand Down
1 change: 1 addition & 0 deletions s3fs/tests/test_s3fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2614,6 +2614,7 @@ async def read_stream():
f = await fs.open_async(fn, mode="rb", block_seze=1000)
while True:
got = await f.read(1000)
assert f.size == len(data)
assert f.tell()
if not got:
break
Expand Down

0 comments on commit 22f8ea1

Please sign in to comment.