-
Notifications
You must be signed in to change notification settings - Fork 914
Support upload directory in s3 transfer manager #2743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c47d69e
Support upload directory in s3 transfer manager
zoewangg 144b16d
address part of the feedback
zoewangg 1b4fa19
Address part of the feedback
zoewangg 0228ea5
Address remaining feedback
zoewangg afae96b
Address part of the feedback
zoewangg 27d2ef6
address feedback
zoewangg 61f0ad3
Address feedback
zoewangg 693b6b9
address feedback
zoewangg 74f4bc7
Merge branch 'master' into zoewang/tmUploadDirectory
zoewangg 0c938c9
Merge branch 'master' into zoewang/tmUploadDirectory
zoewangg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...a/software/amazon/awssdk/transfer/s3/S3TransferManagerUploadDirectoryIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.transfer.s3; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; | ||
import static software.amazon.awssdk.utils.IoUtils.closeQuietly; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import software.amazon.awssdk.core.sync.ResponseTransformer; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.S3Object; | ||
import software.amazon.awssdk.testutils.FileUtils; | ||
import software.amazon.awssdk.utils.Logger; | ||
|
||
public class S3TransferManagerUploadDirectoryIntegrationTest extends S3IntegrationTestBase { | ||
private static final Logger log = Logger.loggerFor(S3TransferManagerUploadDirectoryIntegrationTest.class); | ||
private static final String TEST_BUCKET = temporaryBucketName(S3TransferManagerUploadIntegrationTest.class); | ||
|
||
private static S3TransferManager tm; | ||
private static Path directory; | ||
private static S3Client s3Client; | ||
private static String randomString; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
S3IntegrationTestBase.setUp(); | ||
createBucket(TEST_BUCKET); | ||
randomString = RandomStringUtils.random(100); | ||
directory = createLocalTestDirectory(); | ||
|
||
tm = S3TransferManager.builder() | ||
.s3ClientConfiguration(b -> b.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN) | ||
.region(DEFAULT_REGION) | ||
.maxConcurrency(100)) | ||
.build(); | ||
|
||
s3Client = S3Client.builder() | ||
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN).region(DEFAULT_REGION) | ||
.build(); | ||
} | ||
|
||
@AfterClass | ||
public static void teardown() { | ||
try { | ||
FileUtils.cleanUpTestDirectory(directory); | ||
} catch (Exception exception) { | ||
log.warn(() -> "Failed to clean up test directory " + directory, exception); | ||
} | ||
|
||
try { | ||
deleteBucketAndAllContents(TEST_BUCKET); | ||
} catch (Exception exception) { | ||
log.warn(() -> "Failed to delete s3 bucket " + TEST_BUCKET, exception); | ||
} | ||
|
||
closeQuietly(tm, log.logger()); | ||
closeQuietly(s3Client, log.logger()); | ||
S3IntegrationTestBase.cleanUp(); | ||
} | ||
|
||
@Test | ||
public void uploadDirectory_filesSentCorrectly() { | ||
String prefix = "yolo"; | ||
UploadDirectoryTransfer uploadDirectory = tm.uploadDirectory(u -> u.sourceDirectory(directory) | ||
.bucket(TEST_BUCKET) | ||
.prefix(prefix) | ||
.overrideConfiguration(o -> o.recursive(true))); | ||
CompletedUploadDirectory completedUploadDirectory = uploadDirectory.completionFuture().join(); | ||
assertThat(completedUploadDirectory.failedUploads()).isEmpty(); | ||
|
||
List<String> keys = | ||
s3Client.listObjectsV2Paginator(b -> b.bucket(TEST_BUCKET).prefix(prefix)).contents().stream().map(S3Object::key) | ||
.collect(Collectors.toList()); | ||
|
||
assertThat(keys).containsOnly(prefix + "/bar.txt", prefix + "/foo/1.txt", prefix + "/foo/2.txt"); | ||
|
||
keys.forEach(k -> verifyContent(k, k.substring(prefix.length() + 1) + randomString)); | ||
} | ||
|
||
@Test | ||
public void uploadDirectory_withDelimiter_filesSentCorrectly() { | ||
String prefix = "hello"; | ||
String delimiter = "0"; | ||
UploadDirectoryTransfer uploadDirectory = tm.uploadDirectory(u -> u.sourceDirectory(directory) | ||
.bucket(TEST_BUCKET) | ||
.delimiter(delimiter) | ||
.prefix(prefix) | ||
.overrideConfiguration(o -> o.recursive(true))); | ||
CompletedUploadDirectory completedUploadDirectory = uploadDirectory.completionFuture().join(); | ||
assertThat(completedUploadDirectory.failedUploads()).isEmpty(); | ||
|
||
List<String> keys = | ||
s3Client.listObjectsV2Paginator(b -> b.bucket(TEST_BUCKET).prefix(prefix)).contents().stream().map(S3Object::key) | ||
.collect(Collectors.toList()); | ||
|
||
assertThat(keys).containsOnly(prefix + "0bar.txt", prefix + "0foo01.txt", prefix + "0foo02.txt"); | ||
keys.forEach(k -> { | ||
String path = k.replace(delimiter, "/"); | ||
verifyContent(k, path.substring(prefix.length() + 1) + randomString); | ||
}); | ||
} | ||
|
||
private static Path createLocalTestDirectory() throws IOException { | ||
Path directory = Files.createTempDirectory("test"); | ||
|
||
String directoryName = directory.toString(); | ||
|
||
Files.createDirectory(Paths.get(directory + "/foo")); | ||
Files.write(Paths.get(directoryName, "bar.txt"), ("bar.txt" + randomString).getBytes(StandardCharsets.UTF_8)); | ||
Files.write(Paths.get(directoryName, "foo/1.txt"), ("foo/1.txt" + randomString).getBytes(StandardCharsets.UTF_8)); | ||
Files.write(Paths.get(directoryName, "foo/2.txt"), ("foo/2.txt" + randomString).getBytes(StandardCharsets.UTF_8)); | ||
|
||
return directory; | ||
} | ||
|
||
private static void verifyContent(String key, String expectedContent) { | ||
String actualContent = s3.getObject(r -> r.bucket(TEST_BUCKET).key(key), | ||
ResponseTransformer.toBytes()).asUtf8String(); | ||
|
||
assertThat(actualContent).isEqualTo(expectedContent); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.