Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async write logic #885

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions s3fs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2086,8 +2086,15 @@ async def _invalidate_region_cache(self):
async def open_async(self, path, mode="rb", **kwargs):
if "b" not in mode or kwargs.get("compression"):
raise ValueError
if "w" in mode or "a" in mode:
return await self._open_for_writing(path, mode, **kwargs)
return S3AsyncStreamedFile(self, path, mode)

async def _open_for_writing(self, path, mode, **kwargs):
# Parse the path to get bucket and key
bucket, key, _ = self.split_path(path)
return S3AsyncStreamWriter(self, bucket, key)


class S3File(AbstractBufferedFile):
"""
Expand Down Expand Up @@ -2429,6 +2436,26 @@ def _abort_mpu(self):
self.mpu = None


# Define a new class to represent the file-like object for writing
class S3AsyncStreamWriter:
def __init__(self, s3_fs, bucket, key):
self.s3_fs = s3_fs
self.bucket = bucket
self.key = key
self.closed = False
self.loc = 0

async def write(self, data):
# Write data directly to S3 object
await self.s3_fs._call_s3(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call will overwrite the file on each call to .write(). Ideally, we'd want to be able to push to an open HTTP stream, but that doesn't seem possible. However, s3 allows for multi-part uploads, with the condition that each partc is >=5MB. So we also would need buffering of bytes until there are enough.

"put_object",
Bucket=self.bucket,
Key=self.key,
Body=data,
)
self.loc += len(data)


class S3AsyncStreamedFile(AbstractAsyncStreamedFile):
def __init__(self, fs, path, mode):
self.fs = fs
Expand Down Expand Up @@ -2481,3 +2508,4 @@ async def _call_and_read():
resp["Body"].close()

return await _error_wrapper(_call_and_read, retries=fs.retries)

12 changes: 12 additions & 0 deletions s3fs/tests/test_s3fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,18 @@ async def read_stream():
break
out.append(got)

async def write_stream():
fs = S3FileSystem(
anon=False,
client_kwargs={"endpoint_url": endpoint_uri},
skip_instance_cache=True,
)
await fs._mkdir(test_bucket_name)
f = await fs.open_async(fn, mode="wb")
await f.write(data)
await f.close()

asyncio.run(write_stream())
asyncio.run(read_stream())
assert b"".join(out) == data

Expand Down