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();
}
}
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();
}
}
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();
}
}
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);
}
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()));
}
}
Aggregations