forked from adobe/S3Mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmazonClientUploadIT.java
1145 lines (942 loc) · 44.7 KB
/
AmazonClientUploadIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017-2019 Adobe.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adobe.testing.s3mock.its;
import static java.util.Collections.singletonList;
import static java.util.UUID.randomUUID;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.adobe.testing.s3mock.util.EtagInputStream;
import com.adobe.testing.s3mock.util.HashUtil;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.CopyObjectResult;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.DeleteObjectsResult;
import com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.GetObjectMetadataRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.GetObjectTaggingRequest;
import com.amazonaws.services.s3.model.GetObjectTaggingResult;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.ObjectTagging;
import com.amazonaws.services.s3.model.Owner;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.services.s3.model.SSEAwsKeyManagementParams;
import com.amazonaws.services.s3.model.SetObjectTaggingRequest;
import com.amazonaws.services.s3.model.Tag;
import com.amazonaws.services.s3.transfer.Copy;
import com.amazonaws.services.s3.transfer.Download;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import com.amazonaws.services.s3.transfer.model.CopyResult;
import com.amazonaws.services.s3.transfer.model.UploadResult;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
/**
* Test the application using the AmazonS3 client.
*/
class AmazonClientUploadIT extends S3TestBase {
/**
* Verify that buckets can be created and listed.
*/
@Test
void shouldCreateBucketAndListAllBuckets() {
// the returned creation date might strip off the millisecond-part, resulting in rounding down
// and account for a clock-skew in the Docker container of up to a minute.
final Date creationDate = new Date((System.currentTimeMillis() / 1000) * 1000 - 60000);
final Bucket bucket = s3Client.createBucket(BUCKET_NAME);
assertThat(
String.format("Bucket name should match '%s'!", BUCKET_NAME), bucket.getName(),
equalTo(BUCKET_NAME));
final List<Bucket> buckets =
s3Client.listBuckets().stream().filter(b -> BUCKET_NAME.equals(b.getName()))
.collect(toList());
assertThat("Expecting one bucket", buckets, hasSize(1));
final Bucket createdBucket = buckets.get(0);
assertThat(createdBucket.getCreationDate(), greaterThanOrEqualTo(creationDate));
final Owner bucketOwner = createdBucket.getOwner();
assertThat(bucketOwner.getDisplayName(), equalTo("s3-mock-file-store"));
assertThat(bucketOwner.getId(), equalTo("123"));
}
/**
* Verifies that default Buckets got created after S3 Mock was bootstrapped.
*/
@Test
void defaultBucketsGotCreated() {
final List<Bucket> buckets = s3Client.listBuckets();
final Set<String> bucketNames = buckets.stream().map(Bucket::getName)
.filter(INITIAL_BUCKET_NAMES::contains).collect(Collectors.toSet());
assertThat("Not all default Buckets got created", bucketNames,
is(equalTo(new HashSet<>(INITIAL_BUCKET_NAMES))));
}
/**
* Verifies {@link AmazonS3#doesObjectExist}.
*/
@Test
void putObjectWhereKeyContainsPathFragments() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
final boolean objectExist = s3Client.doesObjectExist(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat(objectExist, is(true));
}
/**
* Stores files in a previously created bucket. List files using ListObjectsV2Request
*
* @throws Exception if FileStreams can not be read
*/
@Test
void shouldUploadAndListV2Objects() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME,
uploadFile.getName(), uploadFile));
s3Client.putObject(new PutObjectRequest(BUCKET_NAME,
uploadFile.getName() + "copy1", uploadFile));
s3Client.putObject(new PutObjectRequest(BUCKET_NAME,
uploadFile.getName() + "copy2", uploadFile));
final ListObjectsV2Request listReq = new ListObjectsV2Request()
.withBucketName(BUCKET_NAME)
.withMaxKeys(3);
final ListObjectsV2Result listResult = s3Client.listObjectsV2(listReq);
assertThat(listResult.getKeyCount(), is(3));
for (final S3ObjectSummary objectSummary : listResult.getObjectSummaries()) {
assertThat(objectSummary.getKey(), containsString(uploadFile.getName()));
final S3Object s3Object = s3Client.getObject(BUCKET_NAME, objectSummary.getKey());
verifyObjectContent(uploadFile, s3Object);
}
}
/**
* Stores a file in a previously created bucket. Downloads the file again and compares checksums
*
* @throws Exception if FileStreams can not be read
*/
@ParameterizedTest(
name = ParameterizedTest.INDEX_PLACEHOLDER + " uploadWithSigning={0}, uploadChunked={1}")
@CsvSource(value = {"true, true", "true, false", "false, true", "false, false"})
void shouldUploadAndDownloadObject(final boolean uploadWithSigning,
final boolean uploadChunked)
throws Exception {
s3Client.createBucket(BUCKET_NAME);
final File uploadFile = new File(UPLOAD_FILE_NAME);
final AmazonS3 uploadClient = defaultTestAmazonS3ClientBuilder()
.withPayloadSigningEnabled(uploadWithSigning)
.withChunkedEncodingDisabled(uploadChunked)
.build();
uploadClient.putObject(new PutObjectRequest(BUCKET_NAME, uploadFile.getName(), uploadFile));
final S3Object s3Object = s3Client.getObject(BUCKET_NAME, uploadFile.getName());
verifyObjectContent(uploadFile, s3Object);
}
/**
* Uses weird, but valid characters in the key used to store an object.
*
* @throws Exception if FileStreams can not be read
*/
@Test
void shouldTolerateWeirdCharactersInObjectKey() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final String weirdStuff = "\\$%&_+.,~|\"':^"
+ "\u1234\uabcd\u0000\u0001"; // non-ascii and unprintable stuff
final String key = weirdStuff + uploadFile.getName() + weirdStuff;
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, key, uploadFile));
final S3Object s3Object = s3Client.getObject(BUCKET_NAME, key);
verifyObjectContent(uploadFile, s3Object);
}
private void verifyObjectContent(final File uploadFile, final S3Object s3Object)
throws NoSuchAlgorithmException, IOException {
final InputStream uploadFileIs = new FileInputStream(uploadFile);
final String uploadHash = HashUtil.getDigest(uploadFileIs);
final String downloadedHash = HashUtil.getDigest(s3Object.getObjectContent());
uploadFileIs.close();
s3Object.close();
assertThat("Up- and downloaded Files should have equal Hashes", uploadHash,
is(equalTo(downloadedHash)));
}
/**
* Uses weird, but valid characters in the key used to store an object. Verifies
* that ListObject returns the correct object names.
*/
@Test
void shouldListWithCorrectObjectNames() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final String weirdStuff = "\\$%&_ .,~|\"':^"
+ "\u1234\uabcd\u0000\u0001"; // non-ascii and unprintable stuff
final String prefix = "shouldListWithCorrectObjectNames/";
final String key = prefix + weirdStuff + uploadFile.getName() + weirdStuff;
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, key, uploadFile));
final ObjectListing listing = s3Client.listObjects(BUCKET_NAME, prefix);
final List<S3ObjectSummary> summaries = listing.getObjectSummaries();
assertThat("Must have exactly one match", summaries, hasSize(1));
assertThat("Object name must match", summaries.get(0).getKey(), equalTo(key));
}
/**
* Same as {@link #shouldListWithCorrectObjectNames()} but for V2 API.
*/
@Test
void shouldListV2WithCorrectObjectNames() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final String weirdStuff = "\\$%&_ .,~|\"':^"
+ "\u1234\uabcd\u0000\u0001"; // non-ascii and unprintable stuff
final String prefix = "shouldListWithCorrectObjectNames/";
final String key = prefix + weirdStuff + uploadFile.getName() + weirdStuff;
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, key, uploadFile));
// AWS client ListObjects V2 defaults to no encoding whereas V1 defaults to URL
final ListObjectsV2Request lorv2 = new ListObjectsV2Request();
lorv2.setBucketName(BUCKET_NAME);
lorv2.setPrefix(prefix);
lorv2.setEncodingType("url"); // do use encoding!
final ListObjectsV2Result listing = s3Client.listObjectsV2(lorv2);
final List<S3ObjectSummary> summaries = listing.getObjectSummaries();
assertThat("Must have exactly one match", summaries, hasSize(1));
assertThat("Object name must match", summaries.get(0).getKey(), equalTo(key));
}
/**
* Uses a key that cannot be represented in XML without encoding. Then lists
* the objects without encoding, expecting a parse exception and thus verifying
* that the encoding parameter is honored.
*
* <p>This isn't the greatest way to test this functionality, however, there
* is currently no low-level testing infrastructure in place.
*/
@Test
void shouldHonorEncodingType() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final String prefix = "shouldHonorEncodingType/";
final String key = prefix + "\u0000"; // key invalid in XML
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, key, uploadFile));
final ListObjectsRequest lor = new ListObjectsRequest(BUCKET_NAME, prefix, null, null, null);
lor.setEncodingType(""); // don't use encoding
// we expect an SdkClientException with a message pointing to XML
// parsing issues.
assertThat(assertThrows(SdkClientException.class, () -> s3Client.listObjects(lor)).getMessage(),
containsString("Failed to parse XML document")
);
}
/**
* The same as {@link #shouldHonorEncodingType()} but for V2 API.
*/
@Test
void shouldHonorEncodingTypeV2() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final String prefix = "shouldHonorEncodingType/";
final String key = prefix + "\u0000"; // key invalid in XML
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, key, uploadFile));
final ListObjectsV2Request lorv2 = new ListObjectsV2Request();
lorv2.setBucketName(BUCKET_NAME);
lorv2.setPrefix(prefix);
lorv2.setEncodingType(""); // don't use encoding
// we expect an SdkClientException with a message pointing to XML
// parsing issues.
assertThat(
assertThrows(SdkClientException.class, () -> s3Client.listObjectsV2(lorv2)).getMessage(),
containsString("Failed to parse XML document")
);
}
/**
* Stores a file in a previously created bucket. Downloads the file again and compares checksums
*
* @throws Exception if FileStreams can not be read
*/
@Test
void shouldUploadAndDownloadStream() throws Exception {
s3Client.createBucket(BUCKET_NAME);
final String resourceId = randomUUID().toString();
final String contentEncoding = "gzip";
final byte[] resource = new byte[]{1, 2, 3, 4, 5};
final ByteArrayInputStream bais = new ByteArrayInputStream(resource);
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(resource.length);
objectMetadata.setContentEncoding(contentEncoding);
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, resourceId, bais, objectMetadata);
final TransferManager tm = createDefaultTransferManager();
final Upload upload = tm.upload(putObjectRequest);
upload.waitForUploadResult();
final S3Object s3Object = s3Client.getObject(BUCKET_NAME, resourceId);
assertThat("Uploaded File should have Encoding-Type set",
s3Object.getObjectMetadata().getContentEncoding(),
is(equalTo(contentEncoding)));
final String uploadHash = HashUtil.getDigest(new ByteArrayInputStream(resource));
final String downloadedHash = HashUtil.getDigest(s3Object.getObjectContent());
s3Object.close();
assertThat("Up- and downloaded Files should have equal Hashes", uploadHash,
is(equalTo(downloadedHash)));
}
/**
* Tests if Object can be uploaded with KMS and Metadata can be retrieved.
*/
@Test
void shouldUploadWithEncryption() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String objectKey = UPLOAD_FILE_NAME;
s3Client.createBucket(BUCKET_NAME);
final ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata("key", "value");
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, objectKey, uploadFile).withMetadata(metadata);
putObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_ENC_KEYREF));
s3Client.putObject(putObjectRequest);
final GetObjectMetadataRequest getObjectMetadataRequest =
new GetObjectMetadataRequest(BUCKET_NAME, objectKey);
final ObjectMetadata objectMetadata = s3Client.getObjectMetadata(getObjectMetadataRequest);
assertThat(objectMetadata.getContentLength(), is(uploadFile.length()));
assertThat("User metadata should be identical!", objectMetadata.getUserMetadata(),
is(equalTo(metadata.getUserMetadata())));
}
/**
* Tests if Object can be uploaded with wrong KMS Key.
*/
@Test
void shouldNotUploadWithWrongEncryptionKey() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile);
putObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_WRONG_KEYREF));
assertThat(
assertThrows(AmazonS3Exception.class, () -> s3Client.putObject(putObjectRequest))
.getMessage(),
containsString("Status Code: 400; Error Code: KMS.NotFoundException"));
}
/**
* Tests if Object can be uploaded with wrong KMS Key.
*/
@Test
void shouldNotUploadStreamingWithWrongEncryptionKey() {
final byte[] bytes = UPLOAD_FILE_NAME.getBytes();
final InputStream stream = new ByteArrayInputStream(bytes);
final String objectKey = randomUUID().toString();
s3Client.createBucket(BUCKET_NAME);
final ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(bytes.length);
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, objectKey, stream, metadata);
putObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_WRONG_KEYREF));
assertThat(
assertThrows(AmazonS3Exception.class, () -> s3Client.putObject(putObjectRequest))
.getMessage(),
containsString("Status Code: 400; Error Code: KMS.NotFoundException"));
}
/**
* Puts an Object; Copies that object to a new bucket; Downloads the object from the new bucket;
* compares checksums of original and copied object.
*
* @throws Exception if an Exception occurs
*/
@Test
void shouldCopyObject() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationbucket";
final String destinationKey = "copyOf/" + sourceKey;
s3Client.createBucket(BUCKET_NAME);
s3Client.createBucket(destinationBucketName);
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
s3Client.copyObject(copyObjectRequest);
final S3Object copiedObject =
s3Client.getObject(destinationBucketName, destinationKey);
final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent());
copiedObject.close();
assertThat("Sourcefile and copied File should have same Hashes",
copiedHash,
is(equalTo(putObjectResult.getETag())));
}
/**
* Puts an Object; Copies that object to a new bucket with new user meta data; Downloads the
* object from the new bucket;
* compares checksums of original and copied object; compares copied object user meta data with
* the new user meta data specified during copy request.
*
* @throws Exception if an Exception occurs
*/
@Test
void shouldCopyObjectWithNewUserMetadata() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationbucket";
final String destinationKey = "copyOf/" + sourceKey + "/withNewUserMetadata";
s3Client.createBucket(BUCKET_NAME);
s3Client.createBucket(destinationBucketName);
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.addUserMetadata("key", "value");
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
copyObjectRequest.setNewObjectMetadata(objectMetadata);
s3Client.copyObject(copyObjectRequest);
final S3Object copiedObject =
s3Client.getObject(destinationBucketName, destinationKey);
final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent());
copiedObject.close();
assertThat("Source file and copied File should have same Hashes",
copiedHash,
is(equalTo(putObjectResult.getETag())));
assertThat("User metadata should be identical!",
copiedObject.getObjectMetadata().getUserMetadata(),
is(equalTo(objectMetadata.getUserMetadata())));
}
/**
* Puts an Object with some user metadata; Copies that object to a new bucket.
* Downloads the object from the new bucket;
* compares checksums of original and copied object; compares copied object user meta data with
* the source object user metadata;
*
* @throws Exception if an Exception occurs
*/
@Test
void shouldCopyObjectWithSourceUserMetadata() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationbucket";
final String destinationKey = "copyOf/" + sourceKey + "/withSourceObjectUserMetadata";
s3Client.createBucket(BUCKET_NAME);
s3Client.createBucket(destinationBucketName);
final ObjectMetadata sourceObjectMetadata = new ObjectMetadata();
sourceObjectMetadata.addUserMetadata("key", "value");
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile);
putObjectRequest.setMetadata(sourceObjectMetadata);
final PutObjectResult putObjectResult =
s3Client.putObject(putObjectRequest);
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
s3Client.copyObject(copyObjectRequest);
final S3Object copiedObject =
s3Client.getObject(destinationBucketName, destinationKey);
final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent());
copiedObject.close();
assertThat("Source file and copied File should have same Hashes",
copiedHash,
is(equalTo(putObjectResult.getETag())));
assertThat("User metadata should be identical!",
copiedObject.getObjectMetadata().getUserMetadata(),
is(equalTo(sourceObjectMetadata.getUserMetadata())));
}
/**
* Copy an object to a key needing URL escaping.
*
* @see #shouldCopyObject()
* @throws Exception if an Exception occurs
*/
@Test
void shouldCopyObjectToKeyNeedingEscaping() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationbucket";
final String destinationKey = "copyOf/some escape-worthy characters %$@ " + sourceKey;
s3Client.createBucket(BUCKET_NAME);
s3Client.createBucket(destinationBucketName);
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
s3Client.copyObject(copyObjectRequest);
final S3Object copiedObject =
s3Client.getObject(destinationBucketName, destinationKey);
final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent());
copiedObject.close();
assertThat("Sourcefile and copied File should have same Hashes",
copiedHash,
is(equalTo(putObjectResult.getETag())));
}
/**
* Copy an object from a key needing URL escaping.
*
* @see #shouldCopyObject()
* @throws Exception if an Exception occurs
*/
@Test
void shouldCopyObjectFromKeyNeedingEscaping() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = "some escape-worthy characters %$@ " + UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationbucket";
final String destinationKey = "copyOf/" + sourceKey;
s3Client.createBucket(BUCKET_NAME);
s3Client.createBucket(destinationBucketName);
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
s3Client.copyObject(copyObjectRequest);
final S3Object copiedObject =
s3Client.getObject(destinationBucketName, destinationKey);
final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent());
copiedObject.close();
assertThat("Sourcefile and copied File should have same Hashes",
copiedHash,
is(equalTo(putObjectResult.getETag())));
}
/**
* Puts an Object; Copies that object to a new bucket; Downloads the object from the new bucket;
* compares checksums of original and copied object.
*
* @throws Exception if an Exception occurs
*/
@Test
void shouldCopyObjectEncrypted() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationbucket";
final String destinationKey = "copyOf/" + sourceKey;
s3Client.createBucket(BUCKET_NAME);
s3Client.createBucket(destinationBucketName);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
copyObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_ENC_KEYREF));
final CopyObjectResult copyObjectResult = s3Client.copyObject(copyObjectRequest);
final ObjectMetadata metadata =
s3Client.getObjectMetadata(destinationBucketName, destinationKey);
final InputStream uploadFileIs = new FileInputStream(uploadFile);
final String uploadHash = HashUtil.getDigest(TEST_ENC_KEYREF, uploadFileIs);
assertThat("ETag should match", copyObjectResult.getETag(), is(uploadHash));
assertThat("Files should have the same length", metadata.getContentLength(),
is(uploadFile.length()));
}
/**
* Tests that an object wont be copied with wrong encryption Key.
*/
@Test
void shouldNotObjectCopyWithWrongEncryptionKey() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
final String sourceKey = UPLOAD_FILE_NAME;
final String destinationBucketName = "destinationbucket";
final String destinationKey = "copyOf" + sourceKey;
s3Client.createBucket(BUCKET_NAME);
s3Client.createBucket(destinationBucketName);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile));
final CopyObjectRequest copyObjectRequest =
new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey);
copyObjectRequest
.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_WRONG_KEYREF));
assertThat(assertThrows(AmazonS3Exception.class, () -> s3Client.copyObject(copyObjectRequest))
.getMessage(),
containsString("Status Code: 400; Error Code: KMS.NotFoundException"));
}
/**
* Creates a bucket and checks if it exists using {@link AmazonS3Client#doesBucketExist(String)}.
*/
@Test
void bucketShouldExist() {
s3Client.createBucket(BUCKET_NAME);
final Boolean doesBucketExist = s3Client.doesBucketExistV2(BUCKET_NAME);
assertThat(String.format("The previously created bucket, '%s', should exist!", BUCKET_NAME),
doesBucketExist,
is(true));
}
/**
* Checks if {@link AmazonS3Client#doesBucketExistV2(String)} is false on a not existing Bucket.
*/
@Test
void bucketShouldNotExist() {
final Boolean doesBucketExist = s3Client.doesBucketExistV2(BUCKET_NAME);
assertThat(String.format("The bucket, '%s', should not exist!", BUCKET_NAME), doesBucketExist,
is(false));
}
/**
* Tests if the Metadata of an existing file can be retrieved.
*/
@Test
void shouldGetObjectMetadata() {
final String nonExistingFileName = "nonExistingFileName";
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.addUserMetadata("key", "value");
objectMetadata.setContentEncoding("gzip");
final PutObjectResult putObjectResult =
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile)
.withMetadata(objectMetadata));
final ObjectMetadata metadataExisting =
s3Client.getObjectMetadata(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat("Content-Encoding should be identical!", metadataExisting.getContentEncoding(),
is(putObjectResult.getMetadata().getContentEncoding()));
assertThat("The ETags should be identical!", metadataExisting.getETag(),
is(putObjectResult.getETag()));
assertThat("User metadata should be identical!", metadataExisting.getUserMetadata(),
is(equalTo(objectMetadata.getUserMetadata())));
assertThat(
assertThrows(AmazonS3Exception.class,
() -> s3Client.getObjectMetadata(BUCKET_NAME, nonExistingFileName)).getMessage(),
containsString("Status Code: 404"));
}
/**
* Tests if an object can be deleted.
*/
@Test
void shouldDeleteObject() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
s3Client.deleteObject(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat(
assertThrows(AmazonS3Exception.class,
() -> s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME)).getMessage(),
containsString("Status Code: 404"));
}
/**
* Tests if an object can be deleted.
*/
@Test
void shouldBatchDeleteObjects() {
final File uploadFile1 = new File(UPLOAD_FILE_NAME);
final File uploadFile2 = new File(UPLOAD_FILE_NAME);
final File uploadFile3 = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final String file1 = "1_" + UPLOAD_FILE_NAME;
final String file2 = "2_" + UPLOAD_FILE_NAME;
final String file3 = "3_" + UPLOAD_FILE_NAME;
final String nonExistingFile = "4_" + randomUUID();
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, file1, uploadFile1));
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, file2, uploadFile2));
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, file3, uploadFile3));
final DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(BUCKET_NAME);
final List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>();
keys.add(new DeleteObjectsRequest.KeyVersion(file1));
keys.add(new DeleteObjectsRequest.KeyVersion(file2));
keys.add(new DeleteObjectsRequest.KeyVersion(file3));
keys.add(new DeleteObjectsRequest.KeyVersion(nonExistingFile));
multiObjectDeleteRequest.setKeys(keys);
final DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
assertThat("Response should contain 3 entries",
delObjRes.getDeletedObjects().size(), is(3));
assertThat("Only existing files were reported as deleted",
delObjRes.getDeletedObjects().stream().map(DeletedObject::getKey).collect(toList()),
contains(file1, file2, file3));
assertThat(assertThrows(AmazonS3Exception.class,
() -> s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME)).getMessage(),
containsString("Status Code: 404"));
}
/**
* Tests that a bucket can be deleted.
*/
@Test
void shouldDeleteBucket() {
s3Client.createBucket(BUCKET_NAME);
s3Client.deleteBucket(BUCKET_NAME);
final Boolean doesBucketExist = s3Client.doesBucketExist(BUCKET_NAME);
assertThat("Deleted Bucket should not exist!", doesBucketExist, is(false));
}
/**
* Tests that a non-empty bucket cannot be deleted.
*/
@Test
void shouldNotDeleteNonEmptyBucket() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
assertThat(assertThrows(AmazonS3Exception.class, () -> s3Client.deleteBucket(BUCKET_NAME))
.getMessage(), containsString("Status Code: 409; Error Code: BucketNotEmpty"));
}
/**
* Tests if the list objects can be retrieved.
*
* <p>For more detailed tests of the List Objects API see {@link ListObjectIT}.
*/
@Test
void shouldGetObjectListing() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
final ObjectListing objectListingResult =
s3Client.listObjects(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat("ObjectListing has no S3Objects.",
objectListingResult.getObjectSummaries().size(),
is(greaterThan(0)));
assertThat("The Name of the first S3ObjectSummary item has not expected the key name.",
objectListingResult.getObjectSummaries().get(0).getKey(),
is(UPLOAD_FILE_NAME));
}
/**
* Tests if an object can be uploaded asynchronously.
*
* @throws Exception not expected.
*/
@Test
void shouldUploadInParallel() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final TransferManager transferManager = createDefaultTransferManager();
final Upload upload =
transferManager.upload(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
final UploadResult uploadResult = upload.waitForUploadResult();
assertThat(uploadResult.getKey(), equalTo(UPLOAD_FILE_NAME));
final S3Object getResult = s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat(getResult.getKey(), equalTo(UPLOAD_FILE_NAME));
}
/**
* Verify that range-downloads work.
*
* @throws Exception not expected
*/
@Test
void checkRangeDownloads() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final TransferManager transferManager = createDefaultTransferManager();
final Upload upload =
transferManager.upload(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
upload.waitForUploadResult();
final File downloadFile = File.createTempFile(randomUUID().toString(), null);
final Download download = transferManager.download(
new GetObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME).withRange(1, 2), downloadFile);
download.waitForCompletion();
assertThat("Invalid file length", downloadFile.length(), is(2L));
assertThat(download.getObjectMetadata().getInstanceLength(), is(uploadFile.length()));
assertThat(download.getObjectMetadata().getContentLength(), is(2L));
transferManager
.download(new GetObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME).withRange(0, 1000),
downloadFile)
.waitForCompletion();
assertThat("Invalid file length", downloadFile.length(), is(uploadFile.length()));
}
/**
* Verifies multipart copy.
*/
@Test
void multipartCopy() throws InterruptedException {
final int contentLen = 3 * _1MB;
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLen);
final String assumedSourceKey = randomUUID().toString();
final Bucket sourceBucket = s3Client.createBucket(randomUUID().toString());
final Bucket targetBucket = s3Client.createBucket(randomUUID().toString());
final TransferManager transferManager = createTransferManager(_2MB, _1MB, _2MB, _1MB);
final EtagInputStream sourceInputStream = new EtagInputStream(
randomInputStream(contentLen), _1MB);
final Upload upload = transferManager
.upload(sourceBucket.getName(), assumedSourceKey, sourceInputStream, objectMetadata);
final UploadResult uploadResult = upload.waitForUploadResult();
assertThat(uploadResult.getKey(), is(assumedSourceKey));
final String assumedDestinationKey = randomUUID().toString();
final Copy copy =
transferManager.copy(sourceBucket.getName(), assumedSourceKey, targetBucket.getName(),
assumedDestinationKey);
final CopyResult copyResult = copy.waitForCopyResult();
assertThat(copyResult.getDestinationKey(), is(assumedDestinationKey));
assertThat("Hashes for source and target S3Object do not match.",
uploadResult.getETag(),
is(sourceInputStream.getEtag()));
}
/**
* Creates a bucket, stores a file, adds tags, retrieves tags and checks them for consistency.
*/
@Test
void shouldAddAndRetrieveTags() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, uploadFile.getName(), uploadFile));
final S3Object s3Object = s3Client.getObject(BUCKET_NAME, uploadFile.getName());
GetObjectTaggingRequest getObjectTaggingRequest = new GetObjectTaggingRequest(BUCKET_NAME,
s3Object.getKey());
GetObjectTaggingResult getObjectTaggingResult = s3Client
.getObjectTagging(getObjectTaggingRequest);
// There shouldn't be any tags here
assertThat("There shouldn't be any tags now", getObjectTaggingResult.getTagSet().size(), is(0));
final List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("foo", "bar"));
final SetObjectTaggingRequest setObjectTaggingRequest =
new SetObjectTaggingRequest(BUCKET_NAME, s3Object.getKey(), new ObjectTagging(tagList));
s3Client.setObjectTagging(setObjectTaggingRequest);
getObjectTaggingRequest = new GetObjectTaggingRequest(BUCKET_NAME, s3Object.getKey());
getObjectTaggingResult = s3Client.getObjectTagging(getObjectTaggingRequest);
// There should be 'foo:bar' here
assertThat("Couldn't find that the tag that was placed",
getObjectTaggingResult.getTagSet().size(), is(1));
assertThat("The value of the tag placed did not match",
getObjectTaggingResult.getTagSet().get(0).getValue(), is("bar"));
}
/**
* Creates a bucket, stores a file with tags, retrieves tags and checks them for consistency.
*/
@Test
void canAddTagsOnPutObject() {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("foo", "bar"));
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, uploadFile.getName(), uploadFile)