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

[Bug bash] Allow ResponseBody.toFile option to override file #3772

Open
wants to merge 6 commits into
base: master
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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-c980953.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Allow ResponseBody.toFile option to override file"
}
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 @@ -100,10 +101,24 @@ default boolean needsConnectionLeftOpen() {
* @return ResponseTransformer instance.
*/
static <ResponseT> ResponseTransformer<ResponseT, ResponseT> toFile(Path path) {
return toFile(path, new StandardCopyOption[0]);
}

/**
* Creates a response transformer that writes all response content to the specified file. Passing
* {@link StandardCopyOption#REPLACE_EXISTING} will prevent a a {@link java.nio.file.FileAlreadyExistsException} to be thrown
* if the file already exists.
*
* @param path Path to file to write to.
* @param <ResponseT> Type of unmarshalled response POJO.
* @param copyOptions options to be passed to the file copy
* @return ResponseTransformer instance.
*/
static <ResponseT> ResponseTransformer<ResponseT, ResponseT> toFile(Path path, StandardCopyOption... copyOptions) {
return (resp, in) -> {
try {
InterruptMonitor.checkInterrupted();
Files.copy(in, path);
Files.copy(in, path, copyOptions);
return resp;
} catch (IOException copyException) {
String copyError = "Failed to read response into file: " + path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.UUID;
import org.junit.Rule;
Expand Down Expand Up @@ -122,6 +123,19 @@ public void downloadToOutputStreamDoesNotRetry() 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, StandardCopyOption.REPLACE_EXISTING));

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

@Test
public void streamingCloseActuallyCloses() throws IOException {
stubForSuccess();
Expand Down