-
Notifications
You must be signed in to change notification settings - Fork 197
aws: support multipart copy for objects larger than 5GB #561
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
acc8cc4
AWS: support copies >5GB using multipart copy
james-rms f4ab018
move comments
james-rms 788237d
use let if instead of min() to avoid overflow
james-rms c908e22
Merge remote-tracking branch 'origin/main' into jrms/aws-multipart-copy
james-rms b5e7385
abort if part upload fails
james-rms 1fdf7d6
test internals
james-rms 231868b
default to single request only
james-rms dbd4724
Apply suggestion from @tustvold
james-rms 6fc900e
fix tests, return error for <5MB part size
james-rms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,64 @@ impl AmazonS3 { | |
| fn path_url(&self, path: &Path) -> String { | ||
| self.client.config.path_url(path) | ||
| } | ||
|
|
||
| /// Perform a multipart copy operation | ||
| /// | ||
| /// If the multipart upload fails, this function makes a best effort attempt to clean it up. | ||
| /// It's the caller's responsibility to add a lifecycle rule if guaranteed cleanup is required, | ||
| /// as we cannot protect against an ill-timed process crash. | ||
| async fn copy_multipart( | ||
| &self, | ||
| from: &Path, | ||
| to: &Path, | ||
| size: u64, | ||
| mode: CompleteMultipartMode, | ||
| ) -> Result<()> { | ||
| // Perform multipart copy using UploadPartCopy | ||
| let upload_id = self | ||
| .client | ||
| .create_multipart(to, PutMultipartOptions::default()) | ||
| .await?; | ||
|
|
||
| // S3 requires minimum 5 MiB per part (except final) and max 10,000 parts | ||
| let part_size = self.client.config.multipart_copy_part_size; | ||
|
|
||
| let mut parts = Vec::new(); | ||
| let mut offset: u64 = 0; | ||
| let mut idx: usize = 0; | ||
| while offset < size { | ||
| let end = if size - offset <= part_size { | ||
| size | ||
| } else { | ||
| offset + part_size | ||
| }; | ||
| let payload = if offset == 0 && end == size { | ||
| PutPartPayload::Copy(from) | ||
| } else { | ||
| PutPartPayload::CopyRange(from, offset..end) | ||
| }; | ||
| let part = match self.client.put_part(to, &upload_id, idx, payload).await { | ||
| Ok(part) => part, | ||
| Err(e) => { | ||
| let _ = self.client.abort_multipart(to, &upload_id).await; | ||
| return Err(e); | ||
| } | ||
| }; | ||
| parts.push(part); | ||
| idx += 1; | ||
| offset = end; | ||
| } | ||
| let res = self | ||
| .client | ||
| .complete_multipart(to, &upload_id, parts, mode) | ||
| .await | ||
| .map(|_| ()); | ||
|
|
||
| if res.is_err() { | ||
| let _ = self.client.abort_multipart(to, &upload_id).await; | ||
| } | ||
| res | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
|
|
@@ -305,14 +363,31 @@ impl ObjectStore for AmazonS3 { | |
| mode, | ||
| extensions: _, | ||
| } = options; | ||
| // Determine source size to decide between single CopyObject and multipart copy | ||
|
Contributor
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. Is there some way we can avoid this, e.g. we try CopyObject normally and on error fallback to multipart? Otherwise this adds an additional S3 roundtrip to every copy request. |
||
| let head_meta = self | ||
| .client | ||
| .get_opts( | ||
| from, | ||
| GetOptions { | ||
| head: true, | ||
| ..Default::default() | ||
| }, | ||
| ) | ||
| .await? | ||
| .meta; | ||
|
|
||
| match mode { | ||
| CopyMode::Overwrite => { | ||
| self.client | ||
| .copy_request(from, to) | ||
| .idempotent(true) | ||
| .send() | ||
| .await?; | ||
| if head_meta.size <= self.client.config.multipart_copy_threshold { | ||
| self.client | ||
| .copy_request(from, to) | ||
| .idempotent(true) | ||
| .send() | ||
| .await?; | ||
| } else { | ||
| self.copy_multipart(from, to, head_meta.size, CompleteMultipartMode::Overwrite) | ||
| .await?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| CopyMode::Create => { | ||
|
|
@@ -322,45 +397,16 @@ impl ObjectStore for AmazonS3 { | |
| } | ||
| Some(S3CopyIfNotExists::HeaderWithStatus(k, v, status)) => (k, v, *status), | ||
| Some(S3CopyIfNotExists::Multipart) => { | ||
| let upload_id = self | ||
| .client | ||
| .create_multipart(to, PutMultipartOptions::default()) | ||
| .await?; | ||
|
|
||
| let res = async { | ||
| let part_id = self | ||
| .client | ||
| .put_part(to, &upload_id, 0, PutPartPayload::Copy(from)) | ||
| .await?; | ||
| match self | ||
| .client | ||
| .complete_multipart( | ||
| to, | ||
| &upload_id, | ||
| vec![part_id], | ||
| CompleteMultipartMode::Create, | ||
| ) | ||
| .await | ||
| { | ||
| Err(e @ Error::Precondition { .. }) => Err(Error::AlreadyExists { | ||
| return self | ||
| .copy_multipart(from, to, head_meta.size, CompleteMultipartMode::Create) | ||
| .await | ||
| .map_err(|err| match err { | ||
| Error::Precondition { .. } => Error::AlreadyExists { | ||
| path: to.to_string(), | ||
| source: Box::new(e), | ||
| }), | ||
| Ok(_) => Ok(()), | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| .await; | ||
|
|
||
| // If the multipart upload failed, make a best effort attempt to | ||
| // clean it up. It's the caller's responsibility to add a | ||
| // lifecycle rule if guaranteed cleanup is required, as we | ||
| // cannot protect against an ill-timed process crash. | ||
| if res.is_err() { | ||
| let _ = self.client.abort_multipart(to, &upload_id).await; | ||
| } | ||
|
|
||
| return res; | ||
| source: Box::new(err), | ||
| }, | ||
| other => other, | ||
| }); | ||
| } | ||
| None => { | ||
| return Err(Error::NotSupported { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
That's a question I have but that I also think should be included in the doc-string: if the object was created using a multi-part upload, are there any requirements on the alignment of the copy-parts or is this irrelevant?
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.
It's not explicitly documented (though there is a note around range requests). There's every chance it's faster to line up the part boundaries exactly, but I haven't tested this directly.
I hadn't thought about it and this bothers me. It doesn't feel good to be opaquely putting part boundaries into a copied object that the user has no visibility into. This also points to the right API being one where the user creates parts directly and keeps track of the boundaries.
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.
FWIW #121