|
| 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 | +import static software.amazon.awssdk.utils.IoUtils.closeQuietly; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import java.util.List; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import org.apache.commons.lang3.RandomStringUtils; |
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | +import software.amazon.awssdk.core.sync.ResponseTransformer; |
| 34 | +import software.amazon.awssdk.services.s3.S3Client; |
| 35 | +import software.amazon.awssdk.services.s3.model.S3Object; |
| 36 | +import software.amazon.awssdk.testutils.FileUtils; |
| 37 | +import software.amazon.awssdk.utils.Logger; |
| 38 | + |
| 39 | +public class S3TransferManagerUploadDirectoryIntegrationTest extends S3IntegrationTestBase { |
| 40 | + private static final Logger log = Logger.loggerFor(S3TransferManagerUploadDirectoryIntegrationTest.class); |
| 41 | + private static final String TEST_BUCKET = temporaryBucketName(S3TransferManagerUploadIntegrationTest.class); |
| 42 | + |
| 43 | + private static S3TransferManager tm; |
| 44 | + private static Path directory; |
| 45 | + private static S3Client s3Client; |
| 46 | + private static String randomString; |
| 47 | + |
| 48 | + @BeforeClass |
| 49 | + public static void setUp() throws Exception { |
| 50 | + S3IntegrationTestBase.setUp(); |
| 51 | + createBucket(TEST_BUCKET); |
| 52 | + randomString = RandomStringUtils.random(100); |
| 53 | + directory = createLocalTestDirectory(); |
| 54 | + |
| 55 | + tm = S3TransferManager.builder() |
| 56 | + .s3ClientConfiguration(b -> b.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN) |
| 57 | + .region(DEFAULT_REGION) |
| 58 | + .maxConcurrency(100)) |
| 59 | + .build(); |
| 60 | + |
| 61 | + s3Client = S3Client.builder() |
| 62 | + .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN).region(DEFAULT_REGION) |
| 63 | + .build(); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterClass |
| 67 | + public static void teardown() { |
| 68 | + try { |
| 69 | + FileUtils.cleanUpTestDirectory(directory); |
| 70 | + } catch (Exception exception) { |
| 71 | + log.warn(() -> "Failed to clean up test directory " + directory, exception); |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + deleteBucketAndAllContents(TEST_BUCKET); |
| 76 | + } catch (Exception exception) { |
| 77 | + log.warn(() -> "Failed to delete s3 bucket " + TEST_BUCKET, exception); |
| 78 | + } |
| 79 | + |
| 80 | + closeQuietly(tm, log.logger()); |
| 81 | + closeQuietly(s3Client, log.logger()); |
| 82 | + S3IntegrationTestBase.cleanUp(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void uploadDirectory_filesSentCorrectly() { |
| 87 | + String prefix = "yolo"; |
| 88 | + UploadDirectoryTransfer uploadDirectory = tm.uploadDirectory(u -> u.sourceDirectory(directory) |
| 89 | + .bucket(TEST_BUCKET) |
| 90 | + .prefix(prefix) |
| 91 | + .overrideConfiguration(o -> o.recursive(true))); |
| 92 | + CompletedUploadDirectory completedUploadDirectory = uploadDirectory.completionFuture().join(); |
| 93 | + assertThat(completedUploadDirectory.failedUploads()).isEmpty(); |
| 94 | + |
| 95 | + List<String> keys = |
| 96 | + s3Client.listObjectsV2Paginator(b -> b.bucket(TEST_BUCKET).prefix(prefix)).contents().stream().map(S3Object::key) |
| 97 | + .collect(Collectors.toList()); |
| 98 | + |
| 99 | + assertThat(keys).containsOnly(prefix + "/bar.txt", prefix + "/foo/1.txt", prefix + "/foo/2.txt"); |
| 100 | + |
| 101 | + keys.forEach(k -> verifyContent(k, k.substring(prefix.length() + 1) + randomString)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void uploadDirectory_withDelimiter_filesSentCorrectly() { |
| 106 | + String prefix = "hello"; |
| 107 | + String delimiter = "0"; |
| 108 | + UploadDirectoryTransfer uploadDirectory = tm.uploadDirectory(u -> u.sourceDirectory(directory) |
| 109 | + .bucket(TEST_BUCKET) |
| 110 | + .delimiter(delimiter) |
| 111 | + .prefix(prefix) |
| 112 | + .overrideConfiguration(o -> o.recursive(true))); |
| 113 | + CompletedUploadDirectory completedUploadDirectory = uploadDirectory.completionFuture().join(); |
| 114 | + assertThat(completedUploadDirectory.failedUploads()).isEmpty(); |
| 115 | + |
| 116 | + List<String> keys = |
| 117 | + s3Client.listObjectsV2Paginator(b -> b.bucket(TEST_BUCKET).prefix(prefix)).contents().stream().map(S3Object::key) |
| 118 | + .collect(Collectors.toList()); |
| 119 | + |
| 120 | + assertThat(keys).containsOnly(prefix + "0bar.txt", prefix + "0foo01.txt", prefix + "0foo02.txt"); |
| 121 | + keys.forEach(k -> { |
| 122 | + String path = k.replace(delimiter, "/"); |
| 123 | + verifyContent(k, path.substring(prefix.length() + 1) + randomString); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + private static Path createLocalTestDirectory() throws IOException { |
| 128 | + Path directory = Files.createTempDirectory("test"); |
| 129 | + |
| 130 | + String directoryName = directory.toString(); |
| 131 | + |
| 132 | + Files.createDirectory(Paths.get(directory + "/foo")); |
| 133 | + Files.write(Paths.get(directoryName, "bar.txt"), ("bar.txt" + randomString).getBytes(StandardCharsets.UTF_8)); |
| 134 | + Files.write(Paths.get(directoryName, "foo/1.txt"), ("foo/1.txt" + randomString).getBytes(StandardCharsets.UTF_8)); |
| 135 | + Files.write(Paths.get(directoryName, "foo/2.txt"), ("foo/2.txt" + randomString).getBytes(StandardCharsets.UTF_8)); |
| 136 | + |
| 137 | + return directory; |
| 138 | + } |
| 139 | + |
| 140 | + private static void verifyContent(String key, String expectedContent) { |
| 141 | + String actualContent = s3.getObject(r -> r.bucket(TEST_BUCKET).key(key), |
| 142 | + ResponseTransformer.toBytes()).asUtf8String(); |
| 143 | + |
| 144 | + assertThat(actualContent).isEqualTo(expectedContent); |
| 145 | + } |
| 146 | +} |
0 commit comments