|
16 | 16 |
|
17 | 17 | package software.amazon.awssdk.services.s3;
|
18 | 18 |
|
| 19 | +import static java.util.Base64.getEncoder; |
19 | 20 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 21 | import static software.amazon.awssdk.services.s3.internal.checksums.ChecksumsEnabledValidator.CHECKSUM;
|
21 | 22 | import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;
|
|
25 | 26 | import java.io.FilterInputStream;
|
26 | 27 | import java.io.IOException;
|
27 | 28 | import java.io.InputStream;
|
| 29 | +import java.io.OutputStream; |
28 | 30 | import java.net.URI;
|
29 | 31 | import java.nio.charset.StandardCharsets;
|
| 32 | +import java.security.DigestInputStream; |
| 33 | +import java.security.MessageDigest; |
| 34 | +import java.security.NoSuchAlgorithmException; |
30 | 35 | import java.util.ArrayList;
|
31 | 36 | import java.util.List;
|
| 37 | +import java.util.Random; |
32 | 38 | import java.util.concurrent.CompletableFuture;
|
33 |
| -import org.junit.AfterClass; |
34 |
| -import org.junit.BeforeClass; |
35 |
| -import org.junit.Test; |
| 39 | +import java.util.stream.Stream; |
| 40 | +import org.junit.jupiter.api.AfterAll; |
| 41 | +import org.junit.jupiter.api.BeforeAll; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | +import org.junit.jupiter.params.ParameterizedTest; |
| 44 | +import org.junit.jupiter.params.provider.Arguments; |
| 45 | +import org.junit.jupiter.params.provider.MethodSource; |
| 46 | +import software.amazon.awssdk.core.ResponseInputStream; |
| 47 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 48 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
36 | 49 | import software.amazon.awssdk.core.async.BlockingInputStreamAsyncRequestBody;
|
| 50 | +import software.amazon.awssdk.core.async.BlockingOutputStreamAsyncRequestBody; |
| 51 | +import software.amazon.awssdk.core.checksums.RequestChecksumCalculation; |
37 | 52 | import software.amazon.awssdk.core.interceptor.Context;
|
38 | 53 | import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
|
39 | 54 | import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
|
40 | 55 | import software.amazon.awssdk.core.sync.RequestBody;
|
41 | 56 | import software.amazon.awssdk.http.ContentStreamProvider;
|
| 57 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
42 | 58 | import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
|
43 | 59 | import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
44 | 60 | import software.amazon.awssdk.services.s3.model.PutObjectResponse;
|
| 61 | +import software.amazon.awssdk.utils.IoUtils; |
45 | 62 |
|
46 | 63 | /**
|
47 | 64 | * Integration tests for {@code PutObject}.
|
48 | 65 | */
|
49 | 66 | public class PutObjectIntegrationTest extends S3IntegrationTestBase {
|
50 | 67 | private static final String BUCKET = temporaryBucketName(PutObjectIntegrationTest.class);
|
51 |
| - private static final String ASYNC_KEY = "async-key"; |
52 |
| - private static final String SYNC_KEY = "sync-key"; |
53 |
| - private static final String TEXT_CONTENT_TYPE = "text/plain"; |
| 68 | + private static final String ASYNC_KEY = "async-key" ; |
| 69 | + private static final String SYNC_KEY = "sync-key" ; |
| 70 | + private static final String TEXT_CONTENT_TYPE = "text/plain" ; |
54 | 71 | private static final byte[] CONTENT = "Hello".getBytes(StandardCharsets.UTF_8);
|
| 72 | + private static final Random RANDOM = new Random(3470); |
55 | 73 |
|
56 |
| - @BeforeClass |
| 74 | + @BeforeAll |
57 | 75 | public static void setUp() throws Exception {
|
58 | 76 | S3IntegrationTestBase.setUp();
|
59 | 77 | createBucket(BUCKET);
|
60 | 78 | }
|
61 | 79 |
|
62 |
| - @AfterClass |
| 80 | + @AfterAll |
63 | 81 | public static void tearDown() {
|
64 | 82 | deleteBucketAndAllContents(BUCKET);
|
65 | 83 | }
|
66 | 84 |
|
| 85 | + public static Stream<Arguments> s3Clients() { |
| 86 | + return Stream.of( |
| 87 | + Arguments.of(s3AsyncClientBuilder().requestChecksumCalculation(RequestChecksumCalculation.WHEN_REQUIRED).build()), |
| 88 | + Arguments.of(s3AsyncClientBuilder().build()), |
| 89 | + Arguments.of(crtClientBuilder().build())); |
| 90 | + } |
| 91 | + |
67 | 92 | @Test
|
68 | 93 | public void objectInputStreamsAreClosed() {
|
69 | 94 | TestContentProvider provider = new TestContentProvider(CONTENT);
|
@@ -97,6 +122,88 @@ public void blockingInputStreamAsyncRequestBody_withContentType_isHonored() {
|
97 | 122 | assertThat(response.contentType()).isEqualTo(TEXT_CONTENT_TYPE);
|
98 | 123 | }
|
99 | 124 |
|
| 125 | + @ParameterizedTest |
| 126 | + @MethodSource("s3Clients") |
| 127 | + public void blockingOutputStreamAsyncRequestBody_writeArrayWithOffset_shouldSucceed(S3AsyncClient client) throws Exception { |
| 128 | + BlockingOutputStreamAsyncRequestBody body = AsyncRequestBody.forBlockingOutputStream(320L); |
| 129 | + |
| 130 | + PutObjectRequest.Builder request = PutObjectRequest.builder() |
| 131 | + .bucket(BUCKET) |
| 132 | + .key(ASYNC_KEY); |
| 133 | + CompletableFuture<PutObjectResponse> responseFuture = client.putObject(request.build(), body); |
| 134 | + MessageDigest expectedDigest = writeArrayWithOffset(body, 32); |
| 135 | + responseFuture.join(); |
| 136 | + |
| 137 | + ResponseInputStream<GetObjectResponse> responseInputStream = |
| 138 | + client.getObject(b -> b.bucket(BUCKET).key(ASYNC_KEY), |
| 139 | + AsyncResponseTransformer.toBlockingInputStream()).join(); |
| 140 | + |
| 141 | + DigestInputStream digestInputStream = new DigestInputStream( |
| 142 | + responseInputStream, MessageDigest.getInstance("SHA-256")); |
| 143 | + IoUtils.drainInputStream(digestInputStream); |
| 144 | + MessageDigest actual = digestInputStream.getMessageDigest(); |
| 145 | + |
| 146 | + assertThat(getEncoder().encodeToString(actual.digest())) |
| 147 | + .isEqualTo(getEncoder().encodeToString(expectedDigest.digest())); |
| 148 | + } |
| 149 | + |
| 150 | + @ParameterizedTest |
| 151 | + @MethodSource("s3Clients") |
| 152 | + public void blockingOutputStreamAsyncRequestBody_writeArray_shouldSucceed(S3AsyncClient client) throws Exception { |
| 153 | + BlockingOutputStreamAsyncRequestBody body = AsyncRequestBody.forBlockingOutputStream(320L); |
| 154 | + |
| 155 | + PutObjectRequest.Builder request = PutObjectRequest.builder() |
| 156 | + .bucket(BUCKET) |
| 157 | + .key(ASYNC_KEY); |
| 158 | + CompletableFuture<PutObjectResponse> responseFuture = client.putObject(request.build(), body); |
| 159 | + MessageDigest expectedDigest = writeArray(body); |
| 160 | + responseFuture.join(); |
| 161 | + |
| 162 | + ResponseInputStream<GetObjectResponse> responseInputStream = |
| 163 | + client.getObject(b -> b.bucket(BUCKET).key(ASYNC_KEY), |
| 164 | + AsyncResponseTransformer.toBlockingInputStream()).join(); |
| 165 | + |
| 166 | + DigestInputStream digestInputStream = new DigestInputStream( |
| 167 | + responseInputStream, MessageDigest.getInstance("SHA-256")); |
| 168 | + IoUtils.drainInputStream(digestInputStream); |
| 169 | + MessageDigest actual = digestInputStream.getMessageDigest(); |
| 170 | + |
| 171 | + assertThat(getEncoder().encodeToString(actual.digest())) |
| 172 | + .isEqualTo(getEncoder().encodeToString(expectedDigest.digest())); |
| 173 | + } |
| 174 | + |
| 175 | + private static MessageDigest writeArray(BlockingOutputStreamAsyncRequestBody body) |
| 176 | + throws NoSuchAlgorithmException { |
| 177 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 178 | + try (OutputStream outputStream = body.outputStream()) { |
| 179 | + byte[] buffer = new byte[32]; |
| 180 | + for (int i = 0; i < 10; i++) { |
| 181 | + RANDOM.nextBytes(buffer); |
| 182 | + digest.update(buffer); |
| 183 | + outputStream.write(buffer); |
| 184 | + } |
| 185 | + } catch (Exception e) { |
| 186 | + throw new RuntimeException(e); |
| 187 | + } |
| 188 | + return digest; |
| 189 | + } |
| 190 | + |
| 191 | + private static MessageDigest writeArrayWithOffset(BlockingOutputStreamAsyncRequestBody body, int chunkSize) |
| 192 | + throws NoSuchAlgorithmException { |
| 193 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 194 | + try (OutputStream outputStream = body.outputStream()) { |
| 195 | + byte[] buffer = new byte[1024]; |
| 196 | + for (int i = 0; i < 10; i++) { |
| 197 | + RANDOM.nextBytes(buffer); |
| 198 | + digest.update(buffer, 10, chunkSize); |
| 199 | + outputStream.write(buffer, 10, chunkSize); |
| 200 | + } |
| 201 | + } catch (Exception e) { |
| 202 | + throw new RuntimeException(e); |
| 203 | + } |
| 204 | + return digest; |
| 205 | + } |
| 206 | + |
100 | 207 | @Test
|
101 | 208 | public void s3Client_usingHttpAndDisableChunkedEncoding() {
|
102 | 209 | try (S3Client s3Client = s3ClientBuilder()
|
|
0 commit comments