Skip to content

Commit ca8aa92

Browse files
authored
Add file overload to DownloadRequest and UploadRequest (#2707)
* Add file overload to DownloadRequest and UploadRequest * Address comments
1 parent c7434cb commit ca8aa92

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "S3 Transfer Manager",
3+
"contributor": "",
4+
"type": "feature",
5+
"description": "Now users can pass `File` to `DownloadRequest` and `UploadRequest` in `S3TransferManager`."
6+
}

services-custom/s3-transfer-manager/src/main/java/software/amazon/awssdk/transfer/s3/DownloadRequest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package software.amazon.awssdk.transfer.s3;
1717

18+
import java.io.File;
1819
import java.nio.file.Path;
1920
import java.util.Objects;
2021
import java.util.function.Consumer;
@@ -112,6 +113,18 @@ public interface Builder extends TransferRequest.Builder<DownloadRequest, Builde
112113
*/
113114
Builder destination(Path destination);
114115

116+
/**
117+
* The file that response contents will be written to. The file must not exist or this method
118+
* will throw an exception. If the file is not writable by the current user then an exception will be thrown.
119+
*
120+
* @param destination the destination path
121+
* @return Returns a reference to this object so that method calls can be chained together.
122+
*/
123+
default Builder destination(File destination) {
124+
Validate.paramNotNull(destination, "destination");
125+
return destination(destination.toPath());
126+
}
127+
115128
/**
116129
* The {@link GetObjectRequest} request that should be used for the download
117130
*

services-custom/s3-transfer-manager/src/main/java/software/amazon/awssdk/transfer/s3/UploadRequest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
import static software.amazon.awssdk.utils.Validate.paramNotNull;
1919

20+
import java.io.File;
2021
import java.nio.file.Path;
2122
import java.util.Objects;
2223
import java.util.function.Consumer;
2324
import software.amazon.awssdk.annotations.NotThreadSafe;
2425
import software.amazon.awssdk.annotations.SdkPreviewApi;
2526
import software.amazon.awssdk.annotations.SdkPublicApi;
2627
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
28+
import software.amazon.awssdk.utils.Validate;
2729
import software.amazon.awssdk.utils.builder.CopyableBuilder;
2830
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
2931

@@ -112,6 +114,19 @@ public interface Builder extends TransferRequest.Builder<UploadRequest, Builder>
112114
*/
113115
Builder source(Path source);
114116

117+
/**
118+
* The file containing data to send to the service. File will be read entirely and may be read
119+
* multiple times in the event of a retry. If the file does not exist or the current user does not have
120+
* access to read it then an exception will be thrown.
121+
*
122+
* @param source the source path
123+
* @return Returns a reference to this object so that method calls can be chained together.
124+
*/
125+
default Builder source(File source) {
126+
Validate.paramNotNull(source, "source");
127+
return this.source(source.toPath());
128+
}
129+
115130
/**
116131
* Configure the {@link PutObjectRequest} that should be used for the upload
117132
*

services-custom/s3-transfer-manager/src/test/java/software/amazon/awssdk/transfer/s3/DownloadRequestTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919

20+
import java.io.File;
21+
import java.nio.file.Path;
2022
import java.nio.file.Paths;
2123
import org.junit.Rule;
2224
import org.junit.Test;
@@ -48,6 +50,29 @@ public void pathMissing_throws() {
4850
.build();
4951
}
5052

53+
@Test
54+
public void usingFile() {
55+
Path path = Paths.get(".");
56+
DownloadRequest requestUsingFile = DownloadRequest.builder()
57+
.getObjectRequest(b -> b.bucket("bucket").key("key"))
58+
.destination(path.toFile())
59+
.build();
60+
61+
assertThat(requestUsingFile.destination()).isEqualTo(path);
62+
}
63+
64+
@Test
65+
public void usingFile_null_shouldThrowException() {
66+
thrown.expect(NullPointerException.class);
67+
thrown.expectMessage("destination");
68+
File file = null;
69+
DownloadRequest.builder()
70+
.getObjectRequest(b -> b.bucket("bucket").key("key"))
71+
.destination(file)
72+
.build();
73+
74+
}
75+
5176
@Test
5277
public void equals_hashcode() {
5378
GetObjectRequest getObjectRequest = GetObjectRequest.builder()

services-custom/s3-transfer-manager/src/test/java/software/amazon/awssdk/transfer/s3/UploadRequestTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919

20+
import java.io.File;
21+
import java.nio.file.Path;
2022
import java.nio.file.Paths;
2123
import org.junit.Rule;
2224
import org.junit.Test;
@@ -46,6 +48,27 @@ public void pathMissing_shouldThrow() {
4648
.build();
4749
}
4850

51+
@Test
52+
public void sourceUsingFile() {
53+
Path path = Paths.get(".");
54+
UploadRequest request = UploadRequest.builder()
55+
.putObjectRequest(b -> b.bucket("bucket").key("key"))
56+
.source(path.toFile())
57+
.build();
58+
assertThat(request.source()).isEqualTo(path);
59+
}
60+
61+
@Test
62+
public void sourceUsingFile_null_shouldThrowException() {
63+
File file = null;
64+
thrown.expect(NullPointerException.class);
65+
thrown.expectMessage("source");
66+
UploadRequest.builder()
67+
.putObjectRequest(b -> b.bucket("bucket").key("key"))
68+
.source(file)
69+
.build();
70+
}
71+
4972
@Test
5073
public void equals_hashcode() {
5174
PutObjectRequest getObjectRequest = PutObjectRequest.builder()

0 commit comments

Comments
 (0)