-
Notifications
You must be signed in to change notification settings - Fork 916
Cross region support for CRT Client #4129
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
joviegas
merged 3 commits into
cross_region_bucket_access/project_branch
from
joviegas/cross_region/crt_client_support
Jun 23, 2023
Merged
Changes from all commits
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 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
156 changes: 156 additions & 0 deletions
156
...3/src/it/java/software/amazon/awssdk/services/s3/crt/S3CrossRegionCrtIntegrationTest.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,156 @@ | ||
/* | ||
* 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.services.s3.crt; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static software.amazon.awssdk.services.s3.crt.S3CrtClientCopyIntegrationTest.randomBytes; | ||
import static software.amazon.awssdk.services.s3.utils.ChecksumUtils.computeCheckSum; | ||
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.file.Files; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.core.ResponseBytes; | ||
import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
import software.amazon.awssdk.core.sync.ResponseTransformer; | ||
import software.amazon.awssdk.crt.CrtResource; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3AsyncClient; | ||
import software.amazon.awssdk.services.s3.S3IntegrationTestBase; | ||
import software.amazon.awssdk.services.s3.model.CopyObjectResponse; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
import software.amazon.awssdk.testutils.RandomTempFile; | ||
import software.amazon.awssdk.testutils.service.AwsTestBase; | ||
|
||
public class S3CrossRegionCrtIntegrationTest extends S3IntegrationTestBase { | ||
public static final Region CROSS_REGION = Region.EU_CENTRAL_1; | ||
private static final String BUCKET = temporaryBucketName(S3CrossRegionCrtIntegrationTest.class); | ||
private static final String KEY = "key"; | ||
private static final String ORIGINAL_OBJ = "test_file.dat"; | ||
private static final String COPIED_OBJ = "test_file_copy.dat"; | ||
private static final long OBJ_SIZE = ThreadLocalRandom.current().nextLong(8 * 1024, 16 * 1024 + 1); | ||
private static S3AsyncClient crtClient; | ||
private static File file; | ||
private static ExecutorService executorService; | ||
|
||
@BeforeAll | ||
public static void setup() throws Exception { | ||
S3IntegrationTestBase.setUp(); | ||
S3IntegrationTestBase.createBucket(BUCKET); | ||
crtClient = S3AsyncClient.crtBuilder() | ||
.region(CROSS_REGION) | ||
.crossRegionAccessEnabled(true) | ||
.credentialsProvider(AwsTestBase.CREDENTIALS_PROVIDER_CHAIN) | ||
.build(); | ||
file = new RandomTempFile(10_000); | ||
S3IntegrationTestBase.s3.putObject(PutObjectRequest.builder() | ||
.bucket(BUCKET) | ||
.key(KEY) | ||
.build(), file.toPath()); | ||
executorService = Executors.newFixedThreadPool(2); | ||
} | ||
|
||
@AfterAll | ||
public static void cleanup() { | ||
crtClient.close(); | ||
S3IntegrationTestBase.deleteBucketAndAllContents(BUCKET); | ||
executorService.shutdown(); | ||
CrtResource.waitForNoResources(); | ||
} | ||
|
||
@Test | ||
void crossRegionClient_getObject() throws IOException { | ||
byte[] bytes = | ||
crtClient.getObject(b -> b.bucket(BUCKET).key(KEY), AsyncResponseTransformer.toBytes()).join().asByteArray(); | ||
assertThat(bytes).isEqualTo(Files.readAllBytes(file.toPath())); | ||
} | ||
|
||
@Test | ||
void putObjectNoSuchBucket() { | ||
assertThatThrownBy(() -> crtClient.getObject(GetObjectRequest.builder().bucket("nonExistingTestBucket" + UUID.randomUUID()).key(KEY).build(), | ||
AsyncResponseTransformer.toBytes()).get()) | ||
.hasCauseInstanceOf(S3Exception.class) | ||
.satisfies(throwable -> assertThat(throwable.getCause()).satisfies(cause -> assertThat(((S3Exception) cause).statusCode()).isEqualTo(404))); | ||
} | ||
|
||
@Test | ||
void copy_copiedObject_hasSameContent() { | ||
byte[] originalContent = randomBytes(OBJ_SIZE); | ||
createOriginalObject(originalContent, ORIGINAL_OBJ); | ||
copyObject(ORIGINAL_OBJ, COPIED_OBJ); | ||
validateCopiedObject(originalContent, ORIGINAL_OBJ); | ||
} | ||
|
||
private void copyObject(String original, String destination) { | ||
CompletableFuture<CopyObjectResponse> future = crtClient.copyObject(c -> c | ||
.sourceBucket(BUCKET) | ||
.sourceKey(original) | ||
.destinationBucket(BUCKET) | ||
.destinationKey(destination)); | ||
|
||
CopyObjectResponse copyObjectResponse = future.join(); | ||
assertThat(copyObjectResponse.responseMetadata().requestId()).isNotNull(); | ||
assertThat(copyObjectResponse.sdkHttpResponse()).isNotNull(); | ||
} | ||
|
||
@Test | ||
void putObject_byteBufferBody_objectSentCorrectly() { | ||
byte[] data = new byte[16384]; | ||
new Random().nextBytes(data); | ||
ByteBuffer byteBuffer = ByteBuffer.wrap(data); | ||
|
||
AsyncRequestBody body = AsyncRequestBody.fromByteBuffer(byteBuffer); | ||
|
||
crtClient.putObject(r -> r.bucket(BUCKET).key(KEY), body).join(); | ||
|
||
ResponseBytes<GetObjectResponse> responseBytes = S3IntegrationTestBase.s3.getObject(r -> r.bucket(BUCKET).key(KEY), | ||
ResponseTransformer.toBytes()); | ||
|
||
byte[] expectedSum = computeCheckSum(byteBuffer); | ||
|
||
assertThat(computeCheckSum(responseBytes.asByteBuffer())).isEqualTo(expectedSum); | ||
} | ||
|
||
private void validateCopiedObject(byte[] originalContent, String originalKey) { | ||
ResponseBytes<GetObjectResponse> copiedObject = s3.getObject(r -> r.bucket(BUCKET) | ||
.key(originalKey), | ||
ResponseTransformer.toBytes()); | ||
assertThat(computeCheckSum(copiedObject.asByteBuffer())).isEqualTo(computeCheckSum(ByteBuffer.wrap(originalContent))); | ||
} | ||
|
||
private void createOriginalObject(byte[] originalContent, String originalKey) { | ||
crtClient.putObject(r -> r.bucket(BUCKET) | ||
.key(originalKey), | ||
AsyncRequestBody.fromBytes(originalContent)).join(); | ||
} | ||
|
||
} |
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
57 changes: 57 additions & 0 deletions
57
services/s3/src/it/java/software/amazon/awssdk/services/s3/crt/TestResponseTransformer.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,57 @@ | ||
/* | ||
* 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.services.s3.crt; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.CompletableFuture; | ||
import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
import software.amazon.awssdk.core.async.SdkPublisher; | ||
import software.amazon.awssdk.http.async.SimpleSubscriber; | ||
import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
|
||
public final class TestResponseTransformer implements AsyncResponseTransformer<GetObjectResponse, Void> { | ||
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. what is the use case for this class? 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. Will remove this from S3CrtGetObjectIntegrationTest |
||
private CompletableFuture<Void> future; | ||
|
||
@Override | ||
public CompletableFuture<Void> prepare() { | ||
future = new CompletableFuture<>(); | ||
return future; | ||
} | ||
|
||
@Override | ||
public void onResponse(GetObjectResponse response) { | ||
assertThat(response).isNotNull(); | ||
} | ||
|
||
@Override | ||
public void onStream(SdkPublisher<ByteBuffer> publisher) { | ||
publisher.subscribe(new SimpleSubscriber(b -> { | ||
}) { | ||
@Override | ||
public void onComplete() { | ||
super.onComplete(); | ||
future.complete(null); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void exceptionOccurred(Throwable error) { | ||
future.completeExceptionally(error); | ||
} | ||
} |
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.