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

Amazon S3: Add option to download to and overwrite existing files #2595

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AmazonS3-a7371a0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "Amazon S3",
"contributor": "mina-asham",
"description": "Add option to download to and overwrite existing files"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.ResponseInputStream;
Expand Down Expand Up @@ -93,17 +94,22 @@ default boolean needsConnectionLeftOpen() {

/**
* Creates a response transformer that writes all response content to the specified file. If the file already exists
* then a {@link java.nio.file.FileAlreadyExistsException} will be thrown.
* and overwrite parameter is set to false then a {@link java.nio.file.FileAlreadyExistsException} will be thrown.
*
* @param path Path to file to write to.
* @param overwrite Overwrite existing files.
* @param <ResponseT> Type of unmarshalled response POJO.
* @return ResponseTransformer instance.
*/
static <ResponseT> ResponseTransformer<ResponseT, ResponseT> toFile(Path path) {
static <ResponseT> ResponseTransformer<ResponseT, ResponseT> toFile(Path path, boolean overwrite) {
return (resp, in) -> {
try {
InterruptMonitor.checkInterrupted();
Files.copy(in, path);
if (overwrite) {
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
} else {
Files.copy(in, path);
}
return resp;
} catch (IOException copyException) {
String copyError = "Failed to read response into file: " + path;
Expand Down Expand Up @@ -133,6 +139,18 @@ static <ResponseT> ResponseTransformer<ResponseT, ResponseT> toFile(Path path) {
};
}

/**
* Creates a response transformer that writes all response content to the specified file. If the file already exists
* then a {@link java.nio.file.FileAlreadyExistsException} will be thrown.
*
* @param path Path to file to write to.
* @param <ResponseT> Type of unmarshalled response POJO.
* @return ResponseTransformer instance.
*/
static <ResponseT> ResponseTransformer<ResponseT, ResponseT> toFile(Path path) {
return toFile(path, false);
}

/**
* Creates a response transformer that writes all response content to the specified file. If the file already exists
* then a {@link java.nio.file.FileAlreadyExistsException} will be thrown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ public void downloadToExistingFileDoesNotRetry() throws IOException {
.isInstanceOf(SdkClientException.class);
}

@Test
public void downloadToExistingFileWithOverwriteSucceeds() throws IOException {
stubForSuccess();

Path tmpFile = Files.createTempFile("overwrite-test.", ".tmp");
tmpFile.toFile().deleteOnExit();

testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), ResponseTransformer.toFile(tmpFile, true));

assertThat(Files.readAllLines(tmpFile)).containsExactly("test \uD83D\uDE02");
}

@Test
public void downloadToOutputStreamDoesNotRetry() throws IOException {
stubForRetriesTimeoutReadingFromStreams();
Expand Down