Search in sources :

Example 41 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class GetObjectAsyncIntegrationTest method customResponseHandler_InterceptorRecievesResponsePojo.

@Test
public void customResponseHandler_InterceptorRecievesResponsePojo() throws Exception {
    final CompletableFuture<String> cf = new CompletableFuture<>();
    try (S3AsyncClient asyncWithInterceptor = createClientWithInterceptor(new AssertingExecutionInterceptor())) {
        String result = asyncWithInterceptor.getObject(getObjectRequest, new AsyncResponseTransformer<GetObjectResponse, String>() {

            @Override
            public CompletableFuture<String> prepare() {
                return cf;
            }

            @Override
            public void onResponse(GetObjectResponse response) {
                // POJO returned by modifyResponse should be delivered to the AsyncResponseTransformer
                assertThat(response.metadata()).hasEntrySatisfying("x-amz-assert", s -> assertThat(s).isEqualTo("injected-value"));
            }

            @Override
            public void onStream(SdkPublisher<ByteBuffer> publisher) {
                publisher.subscribe(new SimpleSubscriber(b -> {
                }) {

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        cf.complete("result");
                    }
                });
            }

            @Override
            public void exceptionOccurred(Throwable throwable) {
                cf.completeExceptionally(throwable);
            }
        }).join();
        assertThat(result).isEqualTo("result");
    }
}
Also used : BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponsePublisher(software.amazon.awssdk.core.async.ResponsePublisher) S3BucketUtils.temporaryBucketName(software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName) CompletableFuture(java.util.concurrent.CompletableFuture) ByteBuffer(java.nio.ByteBuffer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) Context(software.amazon.awssdk.core.interceptor.Context) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) ImmutableMap(software.amazon.awssdk.utils.ImmutableMap) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) SdkResponse(software.amazon.awssdk.core.SdkResponse) RandomTempFile(software.amazon.awssdk.testutils.RandomTempFile) Assert.assertEquals(org.junit.Assert.assertEquals) ClientOverrideConfiguration(software.amazon.awssdk.core.client.config.ClientOverrideConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) SdkPublisher(software.amazon.awssdk.core.async.SdkPublisher) SimpleSubscriber(software.amazon.awssdk.http.async.SimpleSubscriber) Test(org.junit.Test)

Example 42 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class GetObjectWithChecksumTest method async_getObject_with_validation_enabled_no_http_checksum.

@Test
public void async_getObject_with_validation_enabled_no_http_checksum(WireMockRuntimeInfo wm) {
    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(EXAMPLE_RESPONSE_BODY)));
    S3AsyncClient s3Client = getAsyncClientBuilder(wm).overrideConfiguration(o -> o.addExecutionInterceptor(captureChecksumValidationInterceptor)).build();
    String response = s3Client.getObject(r -> r.bucket(EXAMPLE_BUCKET).key("key").checksumMode(ChecksumMode.ENABLED), AsyncResponseTransformer.toBytes()).join().asUtf8String();
    assertThat(response).isEqualTo("Hello world");
    assertThat(captureChecksumValidationInterceptor.responseValidation()).isEqualTo(ChecksumValidation.CHECKSUM_ALGORITHM_NOT_FOUND);
    assertThat(captureChecksumValidationInterceptor.validationAlgorithm()).isNull();
}
Also used : ChecksumMode(software.amazon.awssdk.services.s3.model.ChecksumMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) Function(java.util.function.Function) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) URI(java.net.URI) Region(software.amazon.awssdk.regions.Region) CaptureChecksumValidationInterceptor(software.amazon.awssdk.services.s3.utils.CaptureChecksumValidationInterceptor) ChecksumValidation(software.amazon.awssdk.core.checksums.ChecksumValidation) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) WireMockRuntimeInfo(com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) S3AsyncClientBuilder(software.amazon.awssdk.services.s3.S3AsyncClientBuilder) Algorithm(software.amazon.awssdk.core.checksums.Algorithm) S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.anyUrl(com.github.tomakehurst.wiremock.client.WireMock.anyUrl) BufferedReader(java.io.BufferedReader) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) InputStream(java.io.InputStream) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 43 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class GetObjectWithChecksumTest method async_getObject_with_validation_not_enabled_incorrect_http_checksum.

@Test
public void async_getObject_with_validation_not_enabled_incorrect_http_checksum(WireMockRuntimeInfo wm) {
    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withHeader("x-amz-checksum-crc32", "incorrect").withBody(EXAMPLE_RESPONSE_BODY)));
    S3AsyncClient s3Client = getAsyncClientBuilder(wm).overrideConfiguration(o -> o.addExecutionInterceptor(captureChecksumValidationInterceptor)).build();
    String response = s3Client.getObject(r -> r.bucket(EXAMPLE_BUCKET).key("key"), AsyncResponseTransformer.toBytes()).join().asUtf8String();
    assertThat(response).isEqualTo("Hello world");
    assertThat(captureChecksumValidationInterceptor.responseValidation()).isNull();
    assertThat(captureChecksumValidationInterceptor.validationAlgorithm()).isNull();
}
Also used : ChecksumMode(software.amazon.awssdk.services.s3.model.ChecksumMode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) Function(java.util.function.Function) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) URI(java.net.URI) Region(software.amazon.awssdk.regions.Region) CaptureChecksumValidationInterceptor(software.amazon.awssdk.services.s3.utils.CaptureChecksumValidationInterceptor) ChecksumValidation(software.amazon.awssdk.core.checksums.ChecksumValidation) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) WireMockRuntimeInfo(com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) S3AsyncClientBuilder(software.amazon.awssdk.services.s3.S3AsyncClientBuilder) Algorithm(software.amazon.awssdk.core.checksums.Algorithm) S3ClientBuilder(software.amazon.awssdk.services.s3.S3ClientBuilder) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.anyUrl(com.github.tomakehurst.wiremock.client.WireMock.anyUrl) BufferedReader(java.io.BufferedReader) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) InputStream(java.io.InputStream) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 44 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-sdk-java-v2 by aws.

the class CompleteMultipartUploadFunctionalTest method completeMultipartUpload_asyncClient_errorInResponseBody_invalidErrorXml.

@Test
public void completeMultipartUpload_asyncClient_errorInResponseBody_invalidErrorXml() {
    String bucket = "Example-Bucket";
    String key = "Example-Object";
    String xmlResponseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<Error>\n" + "<SomethingWeird></SomethingWeird>" + "</Error>";
    stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(xmlResponseBody)));
    S3AsyncClient s3Client = getAsyncClientBuilder().build();
    assertThatThrownBy(() -> s3Client.completeMultipartUpload(r -> r.bucket(bucket).key(key).uploadId("upload-id")).join()).isInstanceOf(CompletionException.class).hasCauseInstanceOf(S3Exception.class);
}
Also used : CompletionException(java.util.concurrent.CompletionException) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Test(org.junit.Test)

Example 45 with S3AsyncClient

use of software.amazon.awssdk.services.s3.S3AsyncClient in project flink by splunk.

the class AWSServicesTestUtils method listBucketObjects.

public static List<S3Object> listBucketObjects(S3AsyncClient s3, String bucketName) throws ExecutionException, InterruptedException {
    ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
    CompletableFuture<ListObjectsResponse> res = s3.listObjects(listObjects);
    return res.get().contents();
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse)

Aggregations

S3AsyncClient (software.amazon.awssdk.services.s3.S3AsyncClient)36 Test (org.junit.Test)18 S3AsyncClientBuilder (software.amazon.awssdk.services.s3.S3AsyncClientBuilder)18 Region (software.amazon.awssdk.regions.Region)14 Test (org.junit.jupiter.api.Test)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)11 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)10 WireMock.any (com.github.tomakehurst.wiremock.client.WireMock.any)10 WireMock.anyUrl (com.github.tomakehurst.wiremock.client.WireMock.anyUrl)10 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)10 URI (java.net.URI)10 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)10 StaticCredentialsProvider (software.amazon.awssdk.auth.credentials.StaticCredentialsProvider)10 S3Object (software.amazon.awssdk.services.s3.model.S3Object)9 Context (io.vertx.core.Context)8 VertxTestContext (io.vertx.junit5.VertxTestContext)8 Order (org.junit.jupiter.api.Order)8 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)8 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)8