use of software.amazon.awssdk.services.s3.S3AsyncClient in project boot by workoss.
the class AbstractS3Client method download.
@Override
public byte[] download(String key, Consumer<StorageProgressEvent> consumer) {
key = formatKey(key, false);
try (S3AsyncClient s3AsyncClient = getClient(key, "downloadObject")) {
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(config.getBucketName()).key(key).build();
ResponseBytes<GetObjectResponse> getObjectResponseResponseBytes = s3AsyncClient.getObject(getObjectRequest, AsyncResponseTransformer.toBytes()).get();
return getObjectResponseResponseBytes.asByteArray();
} catch (S3Exception s3Exception) {
throw throwS3Exception(s3Exception);
} catch (Exception e) {
throw new StorageDownloadException("0002", e);
}
}
use of software.amazon.awssdk.services.s3.S3AsyncClient in project boot by workoss.
the class AbstractS3Client method doesObjectExist.
@Override
public boolean doesObjectExist(String key) {
key = formatKey(key, false);
try (S3AsyncClient s3AsyncClient = getClient(key, "doesObjectExist")) {
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder().bucket(config.getBucketName()).key(key).build();
HeadObjectResponse headObjectResponse = s3AsyncClient.headObject(headObjectRequest).get();
} catch (S3Exception s3Exception) {
throw throwS3Exception(s3Exception);
} catch (Exception e) {
throw new StorageException("0002", e);
}
return false;
}
use of software.amazon.awssdk.services.s3.S3AsyncClient in project aws-doc-sdk-examples by awsdocs.
the class S3AsyncStreamOps method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " S3AsyncStreamOps <bucketName> <objectKey> <path>\n\n" + "Where:\n" + " bucketName - the name of the Amazon S3 bucket (for example, bucket1). \n\n" + " objectKey - the name of the object (for example, book.pdf). \n" + " path - the local path to the file (for example, C:/AWS/book.pdf). \n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String objectKey = args[1];
String path = args[2];
Region region = Region.US_WEST_2;
S3AsyncClient client = S3AsyncClient.builder().region(region).build();
GetObjectRequest objectRequest = GetObjectRequest.builder().bucket(bucketName).key(objectKey).build();
CompletableFuture<GetObjectResponse> futureGet = client.getObject(objectRequest, AsyncResponseTransformer.toFile(Paths.get(path)));
futureGet.whenComplete((resp, err) -> {
try {
if (resp != null) {
System.out.println("Object downloaded. Details: " + resp);
} else {
err.printStackTrace();
}
} finally {
// Only close the client when you are completely done with it
client.close();
}
});
futureGet.join();
}
use of software.amazon.awssdk.services.s3.S3AsyncClient in project flink by apache.
the class KinesisFirehoseTableITTest method readFromS3.
private List<Order> readFromS3() throws Exception {
Deadline deadline = Deadline.fromNow(Duration.ofMinutes(1));
List<S3Object> ordersObjects;
List<Order> orders;
do {
Thread.sleep(1000);
ordersObjects = listBucketObjects(s3AsyncClient, BUCKET_NAME);
orders = readObjectsFromS3Bucket(s3AsyncClient, ordersObjects, BUCKET_NAME, responseBytes -> fromJson(new String(responseBytes.asByteArrayUnsafe()), Order.class));
} while (deadline.hasTimeLeft() && orders.size() < NUM_ELEMENTS);
return orders;
}
use of software.amazon.awssdk.services.s3.S3AsyncClient in project flink by apache.
the class KinesisFirehoseSinkITCase method firehoseSinkWritesCorrectDataToMockAWSServices.
@Test
public void firehoseSinkWritesCorrectDataToMockAWSServices() throws Exception {
LOG.info("1 - Creating the bucket for Firehose to deliver into...");
createBucket(s3AsyncClient, BUCKET_NAME);
LOG.info("2 - Creating the IAM Role for Firehose to write into the s3 bucket...");
createIAMRole(iamAsyncClient, ROLE_NAME);
LOG.info("3 - Creating the Firehose delivery stream...");
createDeliveryStream(STREAM_NAME, BUCKET_NAME, ROLE_ARN, firehoseAsyncClient);
KinesisFirehoseSink<String> kdsSink = KinesisFirehoseSink.<String>builder().setSerializationSchema(new SimpleStringSchema()).setDeliveryStreamName(STREAM_NAME).setMaxBatchSize(1).setFirehoseClientProperties(createConfig(mockFirehoseContainer.getEndpoint())).build();
KinesisFirehoseTestUtils.getSampleDataGenerator(env, NUMBER_OF_ELEMENTS).sinkTo(kdsSink);
env.execute("Integration Test");
List<S3Object> objects = listBucketObjects(createS3Client(mockFirehoseContainer.getEndpoint(), httpClient), BUCKET_NAME);
assertThat(objects.size()).isEqualTo(NUMBER_OF_ELEMENTS);
assertThat(readObjectsFromS3Bucket(s3AsyncClient, objects, BUCKET_NAME, response -> new String(response.asByteArrayUnsafe()))).containsAll(KinesisFirehoseTestUtils.getSampleData(NUMBER_OF_ELEMENTS));
}
Aggregations