Search in sources :

Example 21 with S3AsyncClient

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);
    }
}
Also used : PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient)

Example 22 with S3AsyncClient

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;
}
Also used : StorageException(com.workoss.boot.storage.exception.StorageException) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient)

Example 23 with S3AsyncClient

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();
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) Region(software.amazon.awssdk.regions.Region) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient)

Example 24 with S3AsyncClient

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;
}
Also used : IntStream(java.util.stream.IntStream) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Deadline(org.apache.flink.api.common.time.Deadline) BeforeClass(org.junit.BeforeClass) DockerImageName(org.testcontainers.utility.DockerImageName) S3Object(software.amazon.awssdk.services.s3.model.S3Object) AWSServicesTestUtils.createBucket(org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.createBucket) DockerImageVersions(org.apache.flink.util.DockerImageVersions) LoggerFactory(org.slf4j.LoggerFactory) IamAsyncClient(software.amazon.awssdk.services.iam.IamAsyncClient) LocalstackContainer(org.apache.flink.connector.aws.testutils.LocalstackContainer) Network(org.testcontainers.containers.Network) TestUtils(org.apache.flink.tests.util.TestUtils) SdkSystemSetting(software.amazon.awssdk.core.SdkSystemSetting) KinesisFirehoseTestUtils.createFirehoseClient(org.apache.flink.connector.firehose.sink.testutils.KinesisFirehoseTestUtils.createFirehoseClient) KinesisFirehoseTestUtils.createDeliveryStream(org.apache.flink.connector.firehose.sink.testutils.KinesisFirehoseTestUtils.createDeliveryStream) SQLJobSubmission(org.apache.flink.tests.util.flink.SQLJobSubmission) After(org.junit.After) Duration(java.time.Duration) TestLogger(org.apache.flink.util.TestLogger) Timeout(org.junit.rules.Timeout) Assertions(org.assertj.core.api.Assertions) ClassRule(org.junit.ClassRule) SdkAsyncHttpClient(software.amazon.awssdk.http.async.SdkAsyncHttpClient) Path(java.nio.file.Path) Before(org.junit.Before) AWSServicesTestUtils.createIAMRole(org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.createIAMRole) AfterClass(org.junit.AfterClass) AWSServicesTestUtils.createS3Client(org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.createS3Client) Logger(org.slf4j.Logger) AWSServicesTestUtils.listBucketObjects(org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.listBucketObjects) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) AWSServicesTestUtils.createHttpClient(org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.createHttpClient) Paths(java.nio.file.Paths) FirehoseAsyncClient(software.amazon.awssdk.services.firehose.FirehoseAsyncClient) FlinkContainers(org.apache.flink.tests.util.flink.container.FlinkContainers) AWSServicesTestUtils.readObjectsFromS3Bucket(org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.readObjectsFromS3Bucket) AWSServicesTestUtils.createIamClient(org.apache.flink.connector.aws.testutils.AWSServicesTestUtils.createIamClient) Deadline(org.apache.flink.api.common.time.Deadline) S3Object(software.amazon.awssdk.services.s3.model.S3Object)

Example 25 with S3AsyncClient

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));
}
Also used : SimpleStringSchema(org.apache.flink.api.common.serialization.SimpleStringSchema) S3Object(software.amazon.awssdk.services.s3.model.S3Object) Test(org.junit.Test)

Aggregations

S3AsyncClient (software.amazon.awssdk.services.s3.S3AsyncClient)28 Test (org.junit.Test)18 S3AsyncClientBuilder (software.amazon.awssdk.services.s3.S3AsyncClientBuilder)15 Region (software.amazon.awssdk.regions.Region)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)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 S3Client (software.amazon.awssdk.services.s3.S3Client)8 S3ClientBuilder (software.amazon.awssdk.services.s3.S3ClientBuilder)8 Collectors (java.util.stream.Collectors)7 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)7 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)6 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)6 InputStream (java.io.InputStream)5 ByteBuffer (java.nio.ByteBuffer)5