Skip to content

Commit 1a4e832

Browse files
Add S3 Transfer Manager presigned URL download request models (#6356)
* Transfer Manager presigned URL download request models * Fixed checkstyle issues
1 parent 9e6bf61 commit 1a4e832

File tree

4 files changed

+932
-0
lines changed

4 files changed

+932
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.transfer.s3.model;
17+
18+
import java.io.File;
19+
import java.nio.file.Path;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.List;
23+
import java.util.Objects;
24+
import java.util.function.Consumer;
25+
import software.amazon.awssdk.annotations.NotThreadSafe;
26+
import software.amazon.awssdk.annotations.SdkPublicApi;
27+
import software.amazon.awssdk.services.s3.presignedurl.model.PresignedUrlDownloadRequest;
28+
import software.amazon.awssdk.transfer.s3.S3TransferManager;
29+
import software.amazon.awssdk.transfer.s3.progress.TransferListener;
30+
import software.amazon.awssdk.utils.ToString;
31+
import software.amazon.awssdk.utils.Validate;
32+
import software.amazon.awssdk.utils.builder.CopyableBuilder;
33+
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
34+
35+
/**
36+
* Download an object using a pre-signed URL to a local file. For non-file-based downloads, you may use {@link
37+
* PresignedDownloadRequest} instead.
38+
*
39+
* @see S3TransferManager#downloadFileWithPresignedUrl(PresignedDownloadFileRequest)
40+
*/
41+
@SdkPublicApi
42+
public final class PresignedDownloadFileRequest
43+
implements TransferObjectRequest, ToCopyableBuilder<PresignedDownloadFileRequest.Builder, PresignedDownloadFileRequest> {
44+
45+
private final Path destination;
46+
private final PresignedUrlDownloadRequest presignedUrlDownloadRequest;
47+
private final List<TransferListener> transferListeners;
48+
49+
private PresignedDownloadFileRequest(DefaultBuilder builder) {
50+
this.destination = Validate.paramNotNull(builder.destination, "destination");
51+
this.presignedUrlDownloadRequest = Validate.paramNotNull(builder.presignedUrlDownloadRequest,
52+
"presignedUrlDownloadRequest");
53+
this.transferListeners = builder.transferListeners;
54+
}
55+
56+
/**
57+
* Creates a builder that can be used to create a {@link PresignedDownloadFileRequest}.
58+
*
59+
* @see S3TransferManager#downloadFileWithPresignedUrl(PresignedDownloadFileRequest)
60+
*/
61+
public static Builder builder() {
62+
return new DefaultBuilder();
63+
}
64+
65+
@Override
66+
public Builder toBuilder() {
67+
return new DefaultBuilder(this);
68+
}
69+
70+
/**
71+
* The {@link Path} to file that response contents will be written to. The file must not exist or this method
72+
* will throw an exception. If the file is not writable by the current user then an exception will be thrown.
73+
*
74+
* @return the destination path
75+
*/
76+
public Path destination() {
77+
return destination;
78+
}
79+
80+
/**
81+
* @return The {@link PresignedUrlDownloadRequest} request that should be used for the download
82+
*/
83+
public PresignedUrlDownloadRequest presignedUrlDownloadRequest() {
84+
return presignedUrlDownloadRequest;
85+
}
86+
87+
/**
88+
*
89+
* @return List of {@link TransferListener}s that will be notified as part of this request.
90+
*/
91+
@Override
92+
public List<TransferListener> transferListeners() {
93+
return transferListeners;
94+
}
95+
96+
@Override
97+
public boolean equals(Object o) {
98+
if (this == o) {
99+
return true;
100+
}
101+
if (o == null || getClass() != o.getClass()) {
102+
return false;
103+
}
104+
105+
PresignedDownloadFileRequest that = (PresignedDownloadFileRequest) o;
106+
107+
if (!Objects.equals(destination, that.destination)) {
108+
return false;
109+
}
110+
if (!Objects.equals(presignedUrlDownloadRequest, that.presignedUrlDownloadRequest)) {
111+
return false;
112+
}
113+
return Objects.equals(transferListeners, that.transferListeners);
114+
}
115+
116+
@Override
117+
public int hashCode() {
118+
int result = destination != null ? destination.hashCode() : 0;
119+
result = 31 * result + (presignedUrlDownloadRequest != null ? presignedUrlDownloadRequest.hashCode() : 0);
120+
result = 31 * result + (transferListeners != null ? transferListeners.hashCode() : 0);
121+
return result;
122+
}
123+
124+
@Override
125+
public String toString() {
126+
return ToString.builder("PresignedDownloadFileRequest")
127+
.add("destination", destination)
128+
.add("presignedUrlDownloadRequest", presignedUrlDownloadRequest)
129+
.add("transferListeners", transferListeners)
130+
.build();
131+
}
132+
133+
public static Class<? extends Builder> serializableBuilderClass() {
134+
return DefaultBuilder.class;
135+
}
136+
137+
/**
138+
* A builder for a {@link PresignedDownloadFileRequest}, created with {@link #builder()}
139+
*/
140+
@SdkPublicApi
141+
@NotThreadSafe
142+
public interface Builder extends CopyableBuilder<Builder, PresignedDownloadFileRequest> {
143+
144+
/**
145+
* The {@link Path} to file that response contents will be written to. The file must not exist or this method
146+
* will throw an exception. If the file is not writable by the current user then an exception will be thrown.
147+
*
148+
* @param destination the destination path
149+
* @return Returns a reference to this object so that method calls can be chained together.
150+
*/
151+
Builder destination(Path destination);
152+
153+
/**
154+
* The file that response contents will be written to. The file must not exist or this method
155+
* will throw an exception. If the file is not writable by the current user then an exception will be thrown.
156+
*
157+
* @param destination the destination path
158+
* @return Returns a reference to this object so that method calls can be chained together.
159+
*/
160+
default Builder destination(File destination) {
161+
Validate.paramNotNull(destination, "destination");
162+
return destination(destination.toPath());
163+
}
164+
165+
/**
166+
* The {@link PresignedUrlDownloadRequest} request that should be used for the download
167+
*
168+
* @param presignedUrlDownloadRequest the presigned URL download request
169+
* @return a reference to this object so that method calls can be chained together.
170+
* @see #presignedUrlDownloadRequest(Consumer)
171+
*/
172+
Builder presignedUrlDownloadRequest(PresignedUrlDownloadRequest presignedUrlDownloadRequest);
173+
174+
/**
175+
* The {@link PresignedUrlDownloadRequest} request that should be used for the download
176+
*
177+
* <p>
178+
* This is a convenience method that creates an instance of the {@link PresignedUrlDownloadRequest} builder avoiding the
179+
* need to create one manually via {@link PresignedUrlDownloadRequest#builder()}.
180+
*
181+
* @param presignedUrlDownloadRequestBuilder the presigned URL download request
182+
* @return a reference to this object so that method calls can be chained together.
183+
* @see #presignedUrlDownloadRequest(PresignedUrlDownloadRequest)
184+
*/
185+
default Builder presignedUrlDownloadRequest(
186+
Consumer<PresignedUrlDownloadRequest.Builder> presignedUrlDownloadRequestBuilder) {
187+
PresignedUrlDownloadRequest request = PresignedUrlDownloadRequest.builder()
188+
.applyMutation(presignedUrlDownloadRequestBuilder)
189+
.build();
190+
presignedUrlDownloadRequest(request);
191+
return this;
192+
}
193+
194+
/**
195+
* The {@link TransferListener}s that will be notified as part of this request. This method overrides and replaces any
196+
* transferListeners that have already been set. Add an optional request override configuration.
197+
*
198+
* @param transferListeners the collection of transferListeners
199+
* @return Returns a reference to this object so that method calls can be chained together.
200+
* @return This builder for method chaining.
201+
* @see TransferListener
202+
*/
203+
Builder transferListeners(Collection<TransferListener> transferListeners);
204+
205+
/**
206+
* Add a {@link TransferListener} that will be notified as part of this request.
207+
*
208+
* @param transferListener the transferListener to add
209+
* @return Returns a reference to this object so that method calls can be chained together.
210+
* @see TransferListener
211+
*/
212+
Builder addTransferListener(TransferListener transferListener);
213+
214+
}
215+
216+
private static final class DefaultBuilder implements Builder {
217+
private Path destination;
218+
private PresignedUrlDownloadRequest presignedUrlDownloadRequest;
219+
private List<TransferListener> transferListeners;
220+
221+
private DefaultBuilder() {
222+
}
223+
224+
private DefaultBuilder(PresignedDownloadFileRequest presignedDownloadFileRequest) {
225+
this.destination = presignedDownloadFileRequest.destination;
226+
this.presignedUrlDownloadRequest = presignedDownloadFileRequest.presignedUrlDownloadRequest;
227+
this.transferListeners = presignedDownloadFileRequest.transferListeners;
228+
}
229+
230+
@Override
231+
public Builder destination(Path destination) {
232+
this.destination = Validate.paramNotNull(destination, "destination");
233+
return this;
234+
}
235+
236+
public Path getDestination() {
237+
return destination;
238+
}
239+
240+
public void setDestination(Path destination) {
241+
destination(destination);
242+
}
243+
244+
@Override
245+
public DefaultBuilder presignedUrlDownloadRequest(PresignedUrlDownloadRequest presignedUrlDownloadRequest) {
246+
this.presignedUrlDownloadRequest = presignedUrlDownloadRequest;
247+
return this;
248+
}
249+
250+
public PresignedUrlDownloadRequest getPresignedUrlDownloadRequest() {
251+
return presignedUrlDownloadRequest;
252+
}
253+
254+
public void setPresignedUrlDownloadRequest(PresignedUrlDownloadRequest presignedUrlDownloadRequest) {
255+
presignedUrlDownloadRequest(presignedUrlDownloadRequest);
256+
}
257+
258+
@Override
259+
public DefaultBuilder transferListeners(Collection<TransferListener> transferListeners) {
260+
this.transferListeners = transferListeners != null ? new ArrayList<>(transferListeners) : null;
261+
return this;
262+
}
263+
264+
@Override
265+
public Builder addTransferListener(TransferListener transferListener) {
266+
if (transferListeners == null) {
267+
transferListeners = new ArrayList<>();
268+
}
269+
transferListeners.add(transferListener);
270+
return this;
271+
}
272+
273+
public List<TransferListener> getTransferListeners() {
274+
return transferListeners;
275+
}
276+
277+
public void setTransferListeners(Collection<TransferListener> transferListeners) {
278+
transferListeners(transferListeners);
279+
}
280+
281+
@Override
282+
public PresignedDownloadFileRequest build() {
283+
return new PresignedDownloadFileRequest(this);
284+
}
285+
}
286+
}

0 commit comments

Comments
 (0)