-
Notifications
You must be signed in to change notification settings - Fork 274
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
""" | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -2481,3 +2508,4 @@ async def _call_and_read(): | |
resp["Body"].close() | ||
|
||
return await _error_wrapper(_call_and_read, retries=fs.retries) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multipart upload can be supported (I worked at Amazon in the past)
Let's keep the part size as 5.5MB just to be safe.
start the multipart upload session
The response key contains
UploadId
which is the unique identifier to associate with each subsequent operationWe need to start using IO steam to batch up the data
now keep chunking data
Now we just call complete multipart upload
make sure to have this in try-catch block so that in
catch
you can invokeabort_multipart_upload
else your S3 account will keep getting chargedhttps://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have exactly this model for files, where the user API is sync. The idea was, that we should be able to have a true async stream ready for writing, rather than pausing for bigger writes every time we have enough for a new Part. It seems that is not possible.
Implementing what you say - essentially making write() async but keeping the same logic as the standard file - may still be worth while for the sake of fsspec.generic.rsync . GCS and ab2 both support exactly the same pattern.
One possible problem: is it required that a previous Part is finished before the next one can begin?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(also note the method
s3fs.S3FileSystem.clear_multipart_uploads
for dealing with MPUs that might have failed to complete or abort).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's allowed https://aws.amazon.com/blogs/compute/uploading-large-objects-to-amazon-s3-using-multipart-upload-and-transfer-acceleration/