|
| 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.services.s3.crt; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static software.amazon.awssdk.services.s3.crt.S3CrtClientCopyIntegrationTest.randomBytes; |
| 21 | +import static software.amazon.awssdk.services.s3.utils.ChecksumUtils.computeCheckSum; |
| 22 | +import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.ByteBuffer; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.util.Random; |
| 29 | +import java.util.UUID; |
| 30 | +import java.util.concurrent.CompletableFuture; |
| 31 | +import java.util.concurrent.ExecutorService; |
| 32 | +import java.util.concurrent.Executors; |
| 33 | +import java.util.concurrent.ThreadLocalRandom; |
| 34 | +import org.assertj.core.api.Assertions; |
| 35 | +import org.junit.jupiter.api.AfterAll; |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import software.amazon.awssdk.core.ResponseBytes; |
| 39 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 40 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
| 41 | +import software.amazon.awssdk.core.sync.ResponseTransformer; |
| 42 | +import software.amazon.awssdk.crt.CrtResource; |
| 43 | +import software.amazon.awssdk.regions.Region; |
| 44 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 45 | +import software.amazon.awssdk.services.s3.S3IntegrationTestBase; |
| 46 | +import software.amazon.awssdk.services.s3.model.CopyObjectResponse; |
| 47 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 48 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 49 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 50 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
| 51 | +import software.amazon.awssdk.testutils.RandomTempFile; |
| 52 | +import software.amazon.awssdk.testutils.service.AwsTestBase; |
| 53 | + |
| 54 | +public class S3CrossRegionCrtIntegrationTest extends S3IntegrationTestBase { |
| 55 | + public static final Region CROSS_REGION = Region.EU_CENTRAL_1; |
| 56 | + private static final String BUCKET = temporaryBucketName(S3CrossRegionCrtIntegrationTest.class); |
| 57 | + private static final String KEY = "key"; |
| 58 | + private static final String ORIGINAL_OBJ = "test_file.dat"; |
| 59 | + private static final String COPIED_OBJ = "test_file_copy.dat"; |
| 60 | + private static final long OBJ_SIZE = ThreadLocalRandom.current().nextLong(8 * 1024, 16 * 1024 + 1); |
| 61 | + private static S3AsyncClient crtClient; |
| 62 | + private static File file; |
| 63 | + private static ExecutorService executorService; |
| 64 | + |
| 65 | + @BeforeAll |
| 66 | + public static void setup() throws Exception { |
| 67 | + S3IntegrationTestBase.setUp(); |
| 68 | + S3IntegrationTestBase.createBucket(BUCKET); |
| 69 | + crtClient = S3AsyncClient.crtBuilder() |
| 70 | + .region(CROSS_REGION) |
| 71 | + .crossRegionAccessEnabled(true) |
| 72 | + .credentialsProvider(AwsTestBase.CREDENTIALS_PROVIDER_CHAIN) |
| 73 | + .build(); |
| 74 | + file = new RandomTempFile(10_000); |
| 75 | + S3IntegrationTestBase.s3.putObject(PutObjectRequest.builder() |
| 76 | + .bucket(BUCKET) |
| 77 | + .key(KEY) |
| 78 | + .build(), file.toPath()); |
| 79 | + executorService = Executors.newFixedThreadPool(2); |
| 80 | + } |
| 81 | + |
| 82 | + @AfterAll |
| 83 | + public static void cleanup() { |
| 84 | + crtClient.close(); |
| 85 | + S3IntegrationTestBase.deleteBucketAndAllContents(BUCKET); |
| 86 | + executorService.shutdown(); |
| 87 | + CrtResource.waitForNoResources(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void crossRegionClient_getObject() throws IOException { |
| 92 | + byte[] bytes = |
| 93 | + crtClient.getObject(b -> b.bucket(BUCKET).key(KEY), AsyncResponseTransformer.toBytes()).join().asByteArray(); |
| 94 | + assertThat(bytes).isEqualTo(Files.readAllBytes(file.toPath())); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void putObjectNoSuchBucket() { |
| 99 | + assertThatThrownBy(() -> crtClient.getObject(GetObjectRequest.builder().bucket("nonExistingTestBucket" + UUID.randomUUID()).key(KEY).build(), |
| 100 | + AsyncResponseTransformer.toBytes()).get()) |
| 101 | + .hasCauseInstanceOf(S3Exception.class) |
| 102 | + .satisfies(throwable -> assertThat(throwable.getCause()).satisfies(cause -> assertThat(((S3Exception) cause).statusCode()).isEqualTo(404))); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void copy_copiedObject_hasSameContent() { |
| 107 | + byte[] originalContent = randomBytes(OBJ_SIZE); |
| 108 | + createOriginalObject(originalContent, ORIGINAL_OBJ); |
| 109 | + copyObject(ORIGINAL_OBJ, COPIED_OBJ); |
| 110 | + validateCopiedObject(originalContent, ORIGINAL_OBJ); |
| 111 | + } |
| 112 | + |
| 113 | + private void copyObject(String original, String destination) { |
| 114 | + CompletableFuture<CopyObjectResponse> future = crtClient.copyObject(c -> c |
| 115 | + .sourceBucket(BUCKET) |
| 116 | + .sourceKey(original) |
| 117 | + .destinationBucket(BUCKET) |
| 118 | + .destinationKey(destination)); |
| 119 | + |
| 120 | + CopyObjectResponse copyObjectResponse = future.join(); |
| 121 | + assertThat(copyObjectResponse.responseMetadata().requestId()).isNotNull(); |
| 122 | + assertThat(copyObjectResponse.sdkHttpResponse()).isNotNull(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void putObject_byteBufferBody_objectSentCorrectly() { |
| 127 | + byte[] data = new byte[16384]; |
| 128 | + new Random().nextBytes(data); |
| 129 | + ByteBuffer byteBuffer = ByteBuffer.wrap(data); |
| 130 | + |
| 131 | + AsyncRequestBody body = AsyncRequestBody.fromByteBuffer(byteBuffer); |
| 132 | + |
| 133 | + crtClient.putObject(r -> r.bucket(BUCKET).key(KEY), body).join(); |
| 134 | + |
| 135 | + ResponseBytes<GetObjectResponse> responseBytes = S3IntegrationTestBase.s3.getObject(r -> r.bucket(BUCKET).key(KEY), |
| 136 | + ResponseTransformer.toBytes()); |
| 137 | + |
| 138 | + byte[] expectedSum = computeCheckSum(byteBuffer); |
| 139 | + |
| 140 | + assertThat(computeCheckSum(responseBytes.asByteBuffer())).isEqualTo(expectedSum); |
| 141 | + } |
| 142 | + |
| 143 | + private void validateCopiedObject(byte[] originalContent, String originalKey) { |
| 144 | + ResponseBytes<GetObjectResponse> copiedObject = s3.getObject(r -> r.bucket(BUCKET) |
| 145 | + .key(originalKey), |
| 146 | + ResponseTransformer.toBytes()); |
| 147 | + assertThat(computeCheckSum(copiedObject.asByteBuffer())).isEqualTo(computeCheckSum(ByteBuffer.wrap(originalContent))); |
| 148 | + } |
| 149 | + |
| 150 | + private void createOriginalObject(byte[] originalContent, String originalKey) { |
| 151 | + crtClient.putObject(r -> r.bucket(BUCKET) |
| 152 | + .key(originalKey), |
| 153 | + AsyncRequestBody.fromBytes(originalContent)).join(); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments