Search in sources :

Example 1 with GetObjectOutput

use of com.amazonaws.s3.model.GetObjectOutput in project aws-crt-java by awslabs.

the class S3NativeClientTest method testGetObjectVersioned.

@Test
public void testGetObjectVersioned() {
    skipIfNetworkUnavailable();
    try (final EventLoopGroup elGroup = new EventLoopGroup(DEFAULT_NUM_THREADS);
        final HostResolver resolver = new HostResolver(elGroup, DEFAULT_MAX_HOST_ENTRIES);
        final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, resolver);
        final CredentialsProvider provider = getTestCredentialsProvider();
        final S3NativeClient nativeClient = new S3NativeClient(REGION, clientBootstrap, provider, 64_000_000l, 100.)) {
        final long[] length = { 0 };
        nativeClient.getObject(GetObjectRequest.builder().bucket(BUCKET).key(GET_OBJECT_KEY).versionId(GET_OBJECT_VERSION).build(), new ResponseDataConsumer<GetObjectOutput>() {

            @Override
            public void onResponse(GetObjectOutput response) {
                assertNotNull(response);
                assertEquals(response.versionId(), GET_OBJECT_VERSION);
            }

            @Override
            public void onResponseData(ByteBuffer bodyBytesIn) {
                length[0] += bodyBytesIn.remaining();
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onException(final CrtRuntimeException e) {
            }
        }).join();
    }
}
Also used : CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GetObjectOutput(com.amazonaws.s3.model.GetObjectOutput) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with GetObjectOutput

use of com.amazonaws.s3.model.GetObjectOutput in project aws-crt-java by awslabs.

the class S3NativeClientTest method testConcurrentRequests.

@Test
public void testConcurrentRequests() {
    skipIfNetworkUnavailable();
    try (final EventLoopGroup elGroup = new EventLoopGroup(DEFAULT_NUM_THREADS);
        final HostResolver resolver = new HostResolver(elGroup, DEFAULT_MAX_HOST_ENTRIES);
        final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, resolver);
        final CredentialsProvider provider = getTestCredentialsProvider();
        final S3NativeClient nativeClient = new S3NativeClient(REGION, clientBootstrap, provider, 64_000_000l, 10.)) {
        final long[] lengthWritten = { 0 };
        final long contentLength = 1024l;
        final long[] length = { 0 };
        List<CompletableFuture<?>> futures = new ArrayList<CompletableFuture<?>>();
        final int concurrentNum = 20;
        for (int i = 0; i < concurrentNum; i++) {
            futures.add(nativeClient.getObject(GetObjectRequest.builder().bucket(BUCKET).key(GET_OBJECT_KEY).build(), new ResponseDataConsumer<GetObjectOutput>() {

                @Override
                public void onResponse(GetObjectOutput response) {
                    assertNotNull(response);
                }

                @Override
                public void onResponseData(ByteBuffer bodyBytesIn) {
                    length[0] += bodyBytesIn.remaining();
                }

                @Override
                public void onFinished() {
                }

                @Override
                public void onException(final CrtRuntimeException e) {
                }
            }));
            futures.add(nativeClient.putObject(PutObjectRequest.builder().bucket(BUCKET).key(PUT_OBJECT_KEY).contentLength(contentLength).build(), buffer -> {
                while (buffer.hasRemaining()) {
                    buffer.put((byte) 42);
                    ++lengthWritten[0];
                }
                return lengthWritten[0] == contentLength;
            }));
        }
        CompletableFuture<?> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[futures.size()]));
        allFutures.join();
    }
}
Also used : software.amazon.awssdk.crt.io(software.amazon.awssdk.crt.io) BeforeClass(org.junit.BeforeClass) DeleteObjectRequest(com.amazonaws.s3.model.DeleteObjectRequest) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) software.amazon.awssdk.crt.s3(software.amazon.awssdk.crt.s3) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) AwsClientTestFixture(com.amazonaws.test.AwsClientTestFixture) GetObjectRequest(com.amazonaws.s3.model.GetObjectRequest) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) GetObjectOutput(com.amazonaws.s3.model.GetObjectOutput) ArgumentCaptor(org.mockito.ArgumentCaptor) PutObjectOutput(com.amazonaws.s3.model.PutObjectOutput) Duration(java.time.Duration) Map(java.util.Map) Assume(org.junit.Assume) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) ListObjectsRequest(com.amazonaws.s3.model.ListObjectsRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) ListObjectsOutput(com.amazonaws.s3.model.ListObjectsOutput) CancellationException(java.util.concurrent.CancellationException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) PutObjectRequest(com.amazonaws.s3.model.PutObjectRequest) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.any(org.mockito.Mockito.any) Mockito.reset(org.mockito.Mockito.reset) DeleteObjectOutput(com.amazonaws.s3.model.DeleteObjectOutput) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ArrayList(java.util.ArrayList) GetObjectOutput(com.amazonaws.s3.model.GetObjectOutput) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) ByteBuffer(java.nio.ByteBuffer) CompletableFuture(java.util.concurrent.CompletableFuture) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) Test(org.junit.Test)

Example 3 with GetObjectOutput

use of com.amazonaws.s3.model.GetObjectOutput in project aws-crt-java by awslabs.

the class S3NativeClientTest method testGetObjectSpecialCharacters.

@Test
public void testGetObjectSpecialCharacters() {
    skipIfNetworkUnavailable();
    try (final EventLoopGroup elGroup = new EventLoopGroup(DEFAULT_NUM_THREADS);
        final HostResolver resolver = new HostResolver(elGroup, DEFAULT_MAX_HOST_ENTRIES);
        final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, resolver);
        final CredentialsProvider provider = getTestCredentialsProvider();
        final S3NativeClient nativeClient = new S3NativeClient(REGION, clientBootstrap, provider, 64_000_000l, 100.)) {
        final long[] length = { 0 };
        nativeClient.getObject(GetObjectRequest.builder().bucket(BUCKET).key(GET_OBJECT_SPECIAL_CHARACTERS).build(), new ResponseDataConsumer<GetObjectOutput>() {

            @Override
            public void onResponse(GetObjectOutput response) {
                assertNotNull(response);
            }

            @Override
            public void onResponseData(ByteBuffer bodyBytesIn) {
                length[0] += bodyBytesIn.remaining();
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onException(final CrtRuntimeException e) {
            }
        }).join();
    }
}
Also used : CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GetObjectOutput(com.amazonaws.s3.model.GetObjectOutput) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with GetObjectOutput

use of com.amazonaws.s3.model.GetObjectOutput in project aws-crt-java by awslabs.

the class S3NativeClientTest method waitForPropagation.

private void waitForPropagation(final S3NativeClient nativeClient, final String bucket, final String key, final String userMetadataKey) throws Exception {
    final int maxRetries = 5;
    final Duration intervalBetweenRetries = Duration.ofSeconds(5);
    for (int i = 0; i < maxRetries; i++) {
        final GetObjectOutput getObjectOutput = nativeClient.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(), new ResponseDataConsumer<GetObjectOutput>() {

            @Override
            public void onResponse(GetObjectOutput response) {
                assertNotNull(response);
            }

            @Override
            public void onResponseData(ByteBuffer bodyBytesIn) {
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onException(final CrtRuntimeException e) {
            }
        }).get();
        assertNotNull(getObjectOutput);
        final Map<String, String> getMetadata = getObjectOutput.metadata();
        if (getMetadata != null && getMetadata.get(userMetadataKey.toLowerCase()) != null) {
            return;
        }
        Thread.sleep(intervalBetweenRetries.toMillis());
    }
    assertTrue("Propagation timed out for bucket " + bucket + ", key " + key, false);
}
Also used : CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) GetObjectOutput(com.amazonaws.s3.model.GetObjectOutput) Duration(java.time.Duration) ByteBuffer(java.nio.ByteBuffer)

Example 5 with GetObjectOutput

use of com.amazonaws.s3.model.GetObjectOutput in project aws-crt-java by awslabs.

the class S3NativeClientTest method testPutObjectWithUserDefinedMetadata.

@Test
public void testPutObjectWithUserDefinedMetadata() throws Exception {
    skipIfNetworkUnavailable();
    try (final EventLoopGroup elGroup = new EventLoopGroup(DEFAULT_NUM_THREADS);
        final HostResolver resolver = new HostResolver(elGroup, DEFAULT_MAX_HOST_ENTRIES);
        final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, resolver);
        final CredentialsProvider provider = getTestCredentialsProvider();
        final S3NativeClient nativeClient = new S3NativeClient(REGION, clientBootstrap, provider, 64_000_000l, 100.)) {
        final long contentLength = 1024l;
        final long[] lengthWritten = { 0 };
        final String userMetadataKey = "CustomKey1";
        final String userMetadataValue = "SampleValue";
        final Map<String, String> userMetadata = new HashMap<String, String>();
        userMetadata.put(userMetadataKey, userMetadataValue);
        // put with metadata
        nativeClient.putObject(PutObjectRequest.builder().bucket(BUCKET).key(PUT_OBJECT_WITH_METADATA_KEY).contentLength(contentLength).metadata(userMetadata).build(), buffer -> {
            while (buffer.hasRemaining()) {
                buffer.put((byte) 42);
                ++lengthWritten[0];
            }
            return lengthWritten[0] == contentLength;
        }).join();
        // wait propagation
        waitForPropagation(nativeClient, BUCKET, PUT_OBJECT_WITH_METADATA_KEY, userMetadataKey);
        // get
        final long[] length = { 0 };
        final GetObjectOutput getObjectOutput = nativeClient.getObject(GetObjectRequest.builder().bucket(BUCKET).key(PUT_OBJECT_WITH_METADATA_KEY).build(), new ResponseDataConsumer<GetObjectOutput>() {

            @Override
            public void onResponse(GetObjectOutput response) {
                assertNotNull(response);
            }

            @Override
            public void onResponseData(ByteBuffer bodyBytesIn) {
                length[0] += bodyBytesIn.remaining();
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onException(final CrtRuntimeException e) {
            }
        }).get();
        assertNotNull(getObjectOutput);
        final Map<String, String> getMetadata = getObjectOutput.metadata();
        assertNotNull(getMetadata);
        // Amazon S3 stores user-defined metadata keys in lowercase.
        // reference: https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingMetadata.html
        assertEquals(userMetadataValue, getMetadata.get(userMetadataKey.toLowerCase()));
    }
}
Also used : software.amazon.awssdk.crt.io(software.amazon.awssdk.crt.io) BeforeClass(org.junit.BeforeClass) DeleteObjectRequest(com.amazonaws.s3.model.DeleteObjectRequest) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) software.amazon.awssdk.crt.s3(software.amazon.awssdk.crt.s3) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) AwsClientTestFixture(com.amazonaws.test.AwsClientTestFixture) GetObjectRequest(com.amazonaws.s3.model.GetObjectRequest) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) GetObjectOutput(com.amazonaws.s3.model.GetObjectOutput) ArgumentCaptor(org.mockito.ArgumentCaptor) PutObjectOutput(com.amazonaws.s3.model.PutObjectOutput) Duration(java.time.Duration) Map(java.util.Map) Assume(org.junit.Assume) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) ListObjectsRequest(com.amazonaws.s3.model.ListObjectsRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) ListObjectsOutput(com.amazonaws.s3.model.ListObjectsOutput) CancellationException(java.util.concurrent.CancellationException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) PutObjectRequest(com.amazonaws.s3.model.PutObjectRequest) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.any(org.mockito.Mockito.any) Mockito.reset(org.mockito.Mockito.reset) DeleteObjectOutput(com.amazonaws.s3.model.DeleteObjectOutput) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) GetObjectOutput(com.amazonaws.s3.model.GetObjectOutput) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) ByteBuffer(java.nio.ByteBuffer) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) Test(org.junit.Test)

Aggregations

GetObjectOutput (com.amazonaws.s3.model.GetObjectOutput)7 ByteBuffer (java.nio.ByteBuffer)7 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)7 Test (org.junit.Test)6 CredentialsProvider (software.amazon.awssdk.crt.auth.credentials.CredentialsProvider)6 Duration (java.time.Duration)3 CompletionException (java.util.concurrent.CompletionException)3 DeleteObjectOutput (com.amazonaws.s3.model.DeleteObjectOutput)2 DeleteObjectRequest (com.amazonaws.s3.model.DeleteObjectRequest)2 GetObjectRequest (com.amazonaws.s3.model.GetObjectRequest)2 ListObjectsOutput (com.amazonaws.s3.model.ListObjectsOutput)2 ListObjectsRequest (com.amazonaws.s3.model.ListObjectsRequest)2 PutObjectOutput (com.amazonaws.s3.model.PutObjectOutput)2 PutObjectRequest (com.amazonaws.s3.model.PutObjectRequest)2 AwsClientTestFixture (com.amazonaws.test.AwsClientTestFixture)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CancellationException (java.util.concurrent.CancellationException)2