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 ;
17
+
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static software .amazon .awssdk .testutils .service .S3BucketUtils .temporaryBucketName ;
20
+
21
+ import java .io .File ;
22
+ import java .io .IOException ;
23
+ import java .nio .file .Files ;
24
+ import java .nio .file .Path ;
25
+ import java .time .Duration ;
26
+ import org .junit .jupiter .api .AfterAll ;
27
+ import org .junit .jupiter .api .BeforeAll ;
28
+ import org .junit .jupiter .params .ParameterizedTest ;
29
+ import org .junit .jupiter .params .provider .MethodSource ;;
30
+ import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
31
+ import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
32
+ import software .amazon .awssdk .services .s3 .presigner .S3Presigner ;
33
+ import software .amazon .awssdk .services .s3 .presigner .model .GetObjectPresignRequest ;
34
+ import software .amazon .awssdk .services .s3 .presigner .model .PresignedGetObjectRequest ;
35
+ import software .amazon .awssdk .services .s3 .presignedurl .model .PresignedUrlDownloadRequest ;
36
+ import software .amazon .awssdk .testutils .RandomTempFile ;
37
+ import software .amazon .awssdk .transfer .s3 .model .CompletedFileDownload ;
38
+ import software .amazon .awssdk .transfer .s3 .model .FileDownload ;
39
+ import software .amazon .awssdk .transfer .s3 .model .PresignedDownloadFileRequest ;
40
+ import software .amazon .awssdk .transfer .s3 .progress .LoggingTransferListener ;
41
+ import software .amazon .awssdk .utils .Md5Utils ;
42
+
43
+ public class S3TransferManagerPresignedUrlDownloadIntegrationTest extends S3IntegrationTestBase {
44
+ private static final String BUCKET = temporaryBucketName (S3TransferManagerPresignedUrlDownloadIntegrationTest .class );
45
+ private static final String SMALL_KEY = "small-key" ;
46
+ private static final String LARGE_KEY = "large-key" ;
47
+ private static final int SMALL_OBJ_SIZE = 5 * 1024 * 1024 ;
48
+ private static final int LARGE_OBJ_SIZE = 16 * 1024 * 1024 ;
49
+
50
+ private static File smallFile ;
51
+ private static File largeFile ;
52
+ private static S3Presigner presigner ;
53
+
54
+ @ BeforeAll
55
+ public static void setup () throws IOException {
56
+ createBucket (BUCKET );
57
+ smallFile = new RandomTempFile (SMALL_OBJ_SIZE );
58
+ largeFile = new RandomTempFile (LARGE_OBJ_SIZE );
59
+ s3 .putObject (PutObjectRequest .builder ().bucket (BUCKET ).key (SMALL_KEY ).build (), smallFile .toPath ());
60
+ s3 .putObject (PutObjectRequest .builder ().bucket (BUCKET ).key (LARGE_KEY ).build (), largeFile .toPath ());
61
+ presigner = S3Presigner .builder ()
62
+ .region (DEFAULT_REGION )
63
+ .credentialsProvider (CREDENTIALS_PROVIDER_CHAIN )
64
+ .build ();
65
+ }
66
+
67
+ @ AfterAll
68
+ public static void cleanup () {
69
+ if (presigner != null ) {
70
+ presigner .close ();
71
+ }
72
+ deleteBucketAndAllContents (BUCKET );
73
+ }
74
+
75
+ @ ParameterizedTest
76
+ @ MethodSource ("javaTransferManagerOnly" )
77
+ void downloadFileWithPresignedUrl_smallFile_downloadedCorrectly (S3TransferManager tm ) throws Exception {
78
+ PresignedGetObjectRequest presignedRequest = createPresignedRequest (SMALL_KEY );
79
+ Path downloadPath = RandomTempFile .randomUncreatedFile ().toPath ();
80
+ PresignedDownloadFileRequest request = PresignedDownloadFileRequest .builder ()
81
+ .presignedUrlDownloadRequest (PresignedUrlDownloadRequest .builder ()
82
+ .presignedUrl (presignedRequest .url ())
83
+ .build ())
84
+ .destination (downloadPath )
85
+ .addTransferListener (LoggingTransferListener .create ())
86
+ .build ();
87
+
88
+ FileDownload download = tm .downloadFileWithPresignedUrl (request );
89
+ CompletedFileDownload completed = download .completionFuture ().join ();
90
+
91
+ assertThat (Files .exists (downloadPath )).isTrue ();
92
+ assertThat (Md5Utils .md5AsBase64 (downloadPath .toFile ())).isEqualTo (Md5Utils .md5AsBase64 (smallFile ));
93
+ assertThat (completed .response ().responseMetadata ().requestId ()).isNotNull ();
94
+ }
95
+
96
+ @ ParameterizedTest
97
+ @ MethodSource ("javaTransferManagerOnly" )
98
+ void downloadFileWithPresignedUrl_largeFile_downloadedCorrectly (S3TransferManager tm ) throws Exception {
99
+ PresignedGetObjectRequest presignedRequest = createPresignedRequest (LARGE_KEY );
100
+ Path downloadPath = RandomTempFile .randomUncreatedFile ().toPath ();
101
+ PresignedDownloadFileRequest request = PresignedDownloadFileRequest .builder ()
102
+ .presignedUrlDownloadRequest (PresignedUrlDownloadRequest .builder ()
103
+ .presignedUrl (presignedRequest .url ())
104
+ .build ())
105
+ .destination (downloadPath )
106
+ .addTransferListener (LoggingTransferListener .create ())
107
+ .build ();
108
+
109
+ FileDownload download = tm .downloadFileWithPresignedUrl (request );
110
+ CompletedFileDownload completed = download .completionFuture ().join ();
111
+
112
+ assertThat (Files .exists (downloadPath )).isTrue ();
113
+ assertThat (Md5Utils .md5AsBase64 (downloadPath .toFile ())).isEqualTo (Md5Utils .md5AsBase64 (largeFile ));
114
+ assertThat (completed .response ().responseMetadata ().requestId ()).isNotNull ();
115
+ }
116
+
117
+ private static PresignedGetObjectRequest createPresignedRequest (String key ) {
118
+ return presigner .presignGetObject (GetObjectPresignRequest .builder ()
119
+ .signatureDuration (Duration .ofMinutes (10 ))
120
+ .getObjectRequest (GetObjectRequest .builder ()
121
+ .bucket (BUCKET )
122
+ .key (key )
123
+ .build ())
124
+ .build ());
125
+ }
126
+ }
0 commit comments