-
Notifications
You must be signed in to change notification settings - Fork 914
[TM DownloadFile Pause and Resume] Part 1: Add configuration to enable overwriting existing files #3125
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
Merged
zoewangg
merged 4 commits into
zoewang/tm-download-pause-resume
from
zoewang/fileOverwrite
Mar 29, 2022
Merged
[TM DownloadFile Pause and Resume] Part 1: Add configuration to enable overwriting existing files #3125
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cc6cb52
Expose an option to overwrite an existing file in FileAsyncResponseTr…
zoewangg 97cc2a2
Add changelog entries and make TM use CREATE_OR_REPLACE_EXISTING writ…
zoewangg 1576120
Address feedback
zoewangg 307f465
Update and address feedback
zoewangg 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"type": "feature", | ||
"description": "Expose an option in `AsyncResponseTransformer#toFile` to allow overwriting and appending existing file." | ||
} |
6 changes: 6 additions & 0 deletions
6
.changes/next-release/feature-S3TransferManagerDevelperPreview-55da0f7.json
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"category": "S3 Transfer Manager (Develper Preview)", | ||
"contributor": "", | ||
"type": "feature", | ||
"description": "S3TransferManager#downloadFile now by default replaces existing file if it already exists instead of throwing FileAlreadyExistsException. See [#3108](https://github.com/aws/aws-sdk-java-v2/issues/3108)" | ||
} |
209 changes: 209 additions & 0 deletions
209
core/sdk-core/src/main/java/software/amazon/awssdk/core/FileTransformerConfiguration.java
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 |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.core; | ||
|
||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.Path; | ||
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
import software.amazon.awssdk.utils.Validate; | ||
import software.amazon.awssdk.utils.builder.CopyableBuilder; | ||
import software.amazon.awssdk.utils.builder.ToCopyableBuilder; | ||
|
||
/** | ||
* Configuration options for {@link AsyncResponseTransformer#toFile(Path, FileTransformerConfiguration)} to configure how the SDK | ||
* should write the file and if the SDK should delete the file when an exception occurs. | ||
* | ||
* @see #builder() | ||
* @see FileWriteOption | ||
* @see FailureBehavior | ||
*/ | ||
@SdkPublicApi | ||
public final class FileTransformerConfiguration implements ToCopyableBuilder<FileTransformerConfiguration.Builder, | ||
FileTransformerConfiguration> { | ||
private final FileWriteOption fileWriteOption; | ||
private final FailureBehavior failureBehavior; | ||
|
||
private FileTransformerConfiguration(DefaultBuilder builder) { | ||
this.fileWriteOption = Validate.paramNotNull(builder.fileWriteOption, "fileWriteOption"); | ||
this.failureBehavior = Validate.paramNotNull(builder.failureBehavior, "failureBehavior"); | ||
} | ||
|
||
/** | ||
* The configured {@link FileWriteOption} | ||
*/ | ||
public FileWriteOption fileWriteOption() { | ||
return fileWriteOption; | ||
} | ||
|
||
/** | ||
* The configured {@link FailureBehavior} | ||
*/ | ||
public FailureBehavior failureBehavior() { | ||
return failureBehavior; | ||
} | ||
|
||
/** | ||
* Create a {@link Builder}, used to create a {@link FileTransformerConfiguration}. | ||
*/ | ||
public static Builder builder() { | ||
return new DefaultBuilder(); | ||
} | ||
|
||
/** | ||
* Returns the default {@link FileTransformerConfiguration} for {@link FileWriteOption#CREATE_NEW} | ||
* <p> | ||
* Always create a new file. If the file already exists, {@link FileAlreadyExistsException} will be thrown. | ||
* In the event of an error, the SDK will attempt to delete the file (whatever has been written to it so far). | ||
*/ | ||
public static FileTransformerConfiguration defaultCreateNew() { | ||
return builder().fileWriteOption(FileWriteOption.CREATE_NEW) | ||
.failureBehavior(FailureBehavior.DELETE) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Returns the default {@link FileTransformerConfiguration} for {@link FileWriteOption#CREATE_OR_REPLACE_EXISTING} | ||
* <p> | ||
* Create a new file if it doesn't exist, otherwise replace the existing file. | ||
* In the event of an error, the SDK will attempt to delete the file (whatever has been written to it so far). | ||
*/ | ||
public static FileTransformerConfiguration defaultCreateOrReplaceExisting() { | ||
return builder().fileWriteOption(FileWriteOption.CREATE_OR_REPLACE_EXISTING) | ||
.failureBehavior(FailureBehavior.DELETE) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Returns the default {@link FileTransformerConfiguration} for {@link FileWriteOption#CREATE_OR_APPEND_EXISTING} | ||
* <p> | ||
* Create a new file if it doesn't exist, otherwise append to the existing file. | ||
* In the event of an error, the SDK will NOT attempt to delete the file, leaving it as-is | ||
*/ | ||
public static FileTransformerConfiguration defaultCreateOrAppend() { | ||
return builder().fileWriteOption(FileWriteOption.CREATE_OR_APPEND_EXISTING) | ||
.failureBehavior(FailureBehavior.LEAVE) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return new DefaultBuilder().fileWriteOption(fileWriteOption) | ||
.failureBehavior(failureBehavior); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
FileTransformerConfiguration that = (FileTransformerConfiguration) o; | ||
|
||
if (fileWriteOption != that.fileWriteOption) { | ||
return false; | ||
} | ||
return failureBehavior == that.failureBehavior; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = fileWriteOption != null ? fileWriteOption.hashCode() : 0; | ||
result = 31 * result + (failureBehavior != null ? failureBehavior.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
/** | ||
* Defines how the SDK should write the file | ||
*/ | ||
public enum FileWriteOption { | ||
/** | ||
* Always create a new file. If the file already exists, {@link FileAlreadyExistsException} will be thrown. | ||
*/ | ||
CREATE_NEW, | ||
|
||
/** | ||
* Create a new file if it doesn't exist, otherwise replace the existing file. | ||
*/ | ||
CREATE_OR_REPLACE_EXISTING, | ||
|
||
/** | ||
* Create a new file if it doesn't exist, otherwise append to the existing file. | ||
*/ | ||
CREATE_OR_APPEND_EXISTING | ||
} | ||
|
||
/** | ||
* Defines how the SDK should handle the file if there is an exception | ||
*/ | ||
public enum FailureBehavior { | ||
/** | ||
* In the event of an error, the SDK will attempt to delete the file (whatever has been written to it so far). | ||
*/ | ||
DELETE, | ||
|
||
/** | ||
* In the event of an error, the SDK will NOT attempt to delete the file and leave the file as-is (whatever has been | ||
* written to it so far) | ||
*/ | ||
LEAVE | ||
} | ||
|
||
public interface Builder extends CopyableBuilder<Builder, FileTransformerConfiguration> { | ||
|
||
/** | ||
* Configures how to write the file | ||
* | ||
* @param fileWriteOption the file write option | ||
* @return This object for method chaining. | ||
*/ | ||
Builder fileWriteOption(FileWriteOption fileWriteOption); | ||
|
||
/** | ||
* Configures the {@link FailureBehavior} in the event of an error | ||
* | ||
* @param failureBehavior the failure behavior | ||
* @return This object for method chaining. | ||
*/ | ||
Builder failureBehavior(FailureBehavior failureBehavior); | ||
} | ||
|
||
private static class DefaultBuilder implements Builder { | ||
private FileWriteOption fileWriteOption; | ||
private FailureBehavior failureBehavior; | ||
|
||
@Override | ||
public Builder fileWriteOption(FileWriteOption fileWriteOption) { | ||
this.fileWriteOption = fileWriteOption; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder failureBehavior(FailureBehavior failureBehavior) { | ||
this.failureBehavior = failureBehavior; | ||
return this; | ||
} | ||
|
||
@Override | ||
public FileTransformerConfiguration build() { | ||
return new FileTransformerConfiguration(this); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.
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.
Any specific reason for this default?
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.
Yeah, retry would not work as expected if we delete the file when there is an exception.
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.
But it also might not work as expected if we started appending, failed and then on a retry reappended the same data.
Uh oh!
There was an error while loading. Please reload this page.
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.
The initial position is recorded in the ctor, so we will always append on the same offset on retries (overwrite whatever bytes that have been written in the previous attempt)