16
16
package software .amazon .awssdk .transfer .s3 ;
17
17
18
18
import java .nio .file .Path ;
19
+ import java .util .Objects ;
20
+ import java .util .function .Consumer ;
21
+ import software .amazon .awssdk .annotations .NotThreadSafe ;
19
22
import software .amazon .awssdk .annotations .SdkPublicApi ;
20
23
import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
21
24
import software .amazon .awssdk .utils .Validate ;
@@ -31,17 +34,14 @@ public final class DownloadRequest implements TransferRequest, ToCopyableBuilder
31
34
private final GetObjectRequest getObjectRequest ;
32
35
33
36
private DownloadRequest (BuilderImpl builder ) {
34
- Validate .isTrue ((builder .bucket != null && builder .key != null ) ^ builder .getObjectRequest != null ,
35
- "Exactly one of a bucket, key pair or API request must be provided." );
36
37
this .destination = Validate .paramNotNull (builder .destination , "destination" );
37
- this .getObjectRequest = builder .getObjectRequest == null ? GetObjectRequest .builder ()
38
- .bucket (builder .bucket )
39
- .key (builder .key )
40
- .build () : builder .getObjectRequest ;
38
+ this .getObjectRequest = Validate .paramNotNull (builder .getObjectRequest , "getObjectRequest" );
41
39
}
42
40
43
41
/**
44
- * @return A builder for this request.
42
+ * Create a builder that can be used to create a {@link DownloadRequest}.
43
+ *
44
+ * @see S3TransferManager#download(DownloadRequest)
45
45
*/
46
46
public static Builder builder () {
47
47
return new BuilderImpl ();
@@ -52,16 +52,6 @@ public Builder toBuilder() {
52
52
return new BuilderImpl ();
53
53
}
54
54
55
- @ Override
56
- public String bucket () {
57
- return getObjectRequest .bucket ();
58
- }
59
-
60
- @ Override
61
- public String key () {
62
- return getObjectRequest .key ();
63
- }
64
-
65
55
/**
66
56
* The {@link Path} to file that response contents will be written to. The file must not exist or this method
67
57
* will throw an exception. If the file is not writable by the current user then an exception will be thrown.
@@ -72,10 +62,42 @@ public Path destination() {
72
62
return destination ;
73
63
}
74
64
75
- public GetObjectRequest toGetObjectRequest () {
65
+ /**
66
+ * @return The {@link GetObjectRequest} request that should be used for the download
67
+ */
68
+ public GetObjectRequest getObjectRequest () {
76
69
return getObjectRequest ;
77
70
}
78
71
72
+ @ Override
73
+ public boolean equals (Object o ) {
74
+ if (this == o ) {
75
+ return true ;
76
+ }
77
+ if (o == null || getClass () != o .getClass ()) {
78
+ return false ;
79
+ }
80
+
81
+ DownloadRequest that = (DownloadRequest ) o ;
82
+
83
+ if (!Objects .equals (destination , that .destination )) {
84
+ return false ;
85
+ }
86
+ return Objects .equals (getObjectRequest , that .getObjectRequest );
87
+ }
88
+
89
+ @ Override
90
+ public int hashCode () {
91
+ int result = destination != null ? destination .hashCode () : 0 ;
92
+ result = 31 * result + (getObjectRequest != null ? getObjectRequest .hashCode () : 0 );
93
+ return result ;
94
+ }
95
+
96
+ /**
97
+ * A builder for a {@link DownloadRequest}, created with {@link #builder()}
98
+ */
99
+ @ SdkPublicApi
100
+ @ NotThreadSafe
79
101
public interface Builder extends TransferRequest .Builder <DownloadRequest , Builder >, CopyableBuilder <Builder ,
80
102
DownloadRequest > {
81
103
@@ -96,15 +118,28 @@ public interface Builder extends TransferRequest.Builder<DownloadRequest, Builde
96
118
*/
97
119
Builder getObjectRequest (GetObjectRequest getObjectRequest );
98
120
121
+ /**
122
+ * The {@link GetObjectRequest} request that should be used for the download
123
+ *
124
+ * @param getObjectRequestBuilder the getObject request
125
+ * @return a reference to this object so that method calls can be chained together.
126
+ */
127
+ default Builder getObjectRequest (Consumer <GetObjectRequest .Builder > getObjectRequestBuilder ) {
128
+ GetObjectRequest request = GetObjectRequest .builder ()
129
+ .applyMutation (getObjectRequestBuilder )
130
+ .build ();
131
+ getObjectRequest (request );
132
+ return this ;
133
+ }
134
+
135
+
99
136
/**
100
137
* @return The built request.
101
138
*/
102
139
DownloadRequest build ();
103
140
}
104
141
105
142
private static final class BuilderImpl implements Builder {
106
- private String bucket ;
107
- private String key ;
108
143
private Path destination ;
109
144
private GetObjectRequest getObjectRequest ;
110
145
@@ -117,18 +152,6 @@ public Builder destination(Path destination) {
117
152
return this ;
118
153
}
119
154
120
- @ Override
121
- public Builder bucket (String bucket ) {
122
- this .bucket = bucket ;
123
- return this ;
124
- }
125
-
126
- @ Override
127
- public Builder key (String key ) {
128
- this .key = key ;
129
- return this ;
130
- }
131
-
132
155
@ Override
133
156
public Builder getObjectRequest (GetObjectRequest getObjectRequest ) {
134
157
this .getObjectRequest = getObjectRequest ;
0 commit comments