-
Notifications
You must be signed in to change notification settings - Fork 80
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
Added S3 test tools to help with directory uploading #5442
Merged
devinrsmith
merged 3 commits into
deephaven:main
from
devinrsmith:s3-async-testing-transfer-manager
May 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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
91 changes: 91 additions & 0 deletions
91
extensions/s3/src/test/java/io/deephaven/extensions/s3/testlib/S3Helper.java
This file contains 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,91 @@ | ||||||
// | ||||||
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending | ||||||
// | ||||||
package io.deephaven.extensions.s3.testlib; | ||||||
|
||||||
import software.amazon.awssdk.services.s3.S3AsyncClient; | ||||||
import software.amazon.awssdk.services.s3.model.Delete; | ||||||
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; | ||||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; | ||||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; | ||||||
import software.amazon.awssdk.services.s3.model.ObjectIdentifier; | ||||||
import software.amazon.awssdk.services.s3.model.S3Object; | ||||||
import software.amazon.awssdk.transfer.s3.S3TransferManager; | ||||||
import software.amazon.awssdk.transfer.s3.model.CompletedDirectoryUpload; | ||||||
import software.amazon.awssdk.transfer.s3.model.DirectoryUpload; | ||||||
import software.amazon.awssdk.transfer.s3.model.UploadDirectoryRequest; | ||||||
|
||||||
import java.net.URISyntaxException; | ||||||
import java.net.URL; | ||||||
import java.nio.file.Path; | ||||||
import java.time.Duration; | ||||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
import java.util.concurrent.CompletableFuture; | ||||||
import java.util.concurrent.ExecutionException; | ||||||
import java.util.concurrent.TimeUnit; | ||||||
import java.util.concurrent.TimeoutException; | ||||||
import java.util.stream.Collectors; | ||||||
|
||||||
public final class S3Helper { | ||||||
public static void uploadDirectory( | ||||||
S3AsyncClient s3AsyncClient, | ||||||
URL resourceDir, | ||||||
String bucket, | ||||||
String prefix, | ||||||
Duration timeout) throws URISyntaxException, ExecutionException, InterruptedException, TimeoutException { | ||||||
try (final S3TransferManager manager = S3TransferManager.builder().s3Client(s3AsyncClient).build()) { | ||||||
uploadDirectory(manager, resourceDir, bucket, prefix, timeout); | ||||||
} | ||||||
} | ||||||
|
||||||
public static void uploadDirectory( | ||||||
S3TransferManager transferManager, | ||||||
URL resourceDir, | ||||||
String bucket, | ||||||
String prefix, | ||||||
Duration timeout) throws URISyntaxException, ExecutionException, InterruptedException, TimeoutException { | ||||||
final Path dir = Path.of(resourceDir.toURI()); | ||||||
// Not a way to get a list of the uploaded files, even when using a TransferListener. | ||||||
final DirectoryUpload directoryUpload = transferManager.uploadDirectory(UploadDirectoryRequest.builder() | ||||||
.source(dir) | ||||||
.bucket(bucket) | ||||||
.s3Prefix(prefix) | ||||||
.build()); | ||||||
final CompletedDirectoryUpload upload = | ||||||
directoryUpload.completionFuture().get(timeout.toNanos(), TimeUnit.NANOSECONDS); | ||||||
if (!upload.failedTransfers().isEmpty()) { | ||||||
throw new RuntimeException("Upload has failed transfers"); | ||||||
} | ||||||
} | ||||||
|
||||||
public static void deleteAllKeys(S3AsyncClient s3AsyncClient, String bucket) | ||||||
throws ExecutionException, InterruptedException, TimeoutException { | ||||||
ListObjectsV2Response response = s3AsyncClient | ||||||
.listObjectsV2(ListObjectsV2Request.builder().bucket(bucket).build()).get(5, TimeUnit.SECONDS); | ||||||
final List<CompletableFuture<?>> futures = new ArrayList<>(); | ||||||
while (true) { | ||||||
final List<ObjectIdentifier> deletes = response.contents() | ||||||
.stream() | ||||||
.map(S3Object::key) | ||||||
.map(S3Helper::objectId) | ||||||
.collect(Collectors.toList()); | ||||||
futures.add(s3AsyncClient.deleteObjects(DeleteObjectsRequest.builder() | ||||||
.bucket(bucket) | ||||||
.delete(Delete.builder().objects(deletes).build()) | ||||||
.build())); | ||||||
final String nextContinuationToken = response.nextContinuationToken(); | ||||||
if (nextContinuationToken == null) { | ||||||
break; | ||||||
} | ||||||
response = s3AsyncClient.listObjectsV2( | ||||||
ListObjectsV2Request.builder().bucket(bucket).continuationToken(nextContinuationToken).build()) | ||||||
.get(5, TimeUnit.SECONDS); | ||||||
} | ||||||
CompletableFuture.allOf(futures.stream().toArray(CompletableFuture[]::new)).get(5, TimeUnit.SECONDS); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
private static ObjectIdentifier objectId(String o) { | ||||||
return ObjectIdentifier.builder().key(o).build(); | ||||||
} | ||||||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More a question than a comment: is there a way to upload a set of files and share between individual tests? Am I reading this correctly that each test must do its own setup / upload?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's just how this specific test orchestrates them. You can definitely upload once per class (
org.junit.jupiter.api.BeforeAll
JUnit 5,org.junit.BeforeClass
JUnit 4), or even statically once per JVM.