Search in sources :

Example 26 with S3AsyncClient

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

the class VertxS3ClientSpec method createS3Bucket.

@Test
@Order(1)
@Timeout(value = 60, timeUnit = TimeUnit.SECONDS)
void createS3Bucket(Vertx vertx, VertxTestContext ctx) throws Exception {
    final Context originalContext = vertx.getOrCreateContext();
    final S3AsyncClient s3 = s3(originalContext);
    single(s3.createBucket(VertxS3ClientSpec::createBucketReq)).subscribe(createRes -> {
        assertContext(vertx, originalContext, ctx);
        ctx.completeNow();
    }, ctx::failNow);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Context(io.vertx.core.Context) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 27 with S3AsyncClient

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

the class VertxS3ClientSpec method downloadImageFromBucketWithoutSettingResponseHandler.

@Test
@Order(7)
void downloadImageFromBucketWithoutSettingResponseHandler(Vertx vertx, VertxTestContext ctx) throws Exception {
    final Context originalContext = vertx.getOrCreateContext();
    final S3AsyncClient s3 = s3(originalContext);
    final Buffer received = Buffer.buffer();
    AtomicBoolean handlerCalled = new AtomicBoolean(false);
    VertxAsyncResponseTransformer<GetObjectResponse> transformer = new VertxAsyncResponseTransformer<>(new WriteStream<Buffer>() {

        @Override
        public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
            return null;
        }

        @Override
        public Future<Void> write(Buffer data) {
            received.appendBuffer(data);
            return Future.succeededFuture();
        }

        @Override
        public void write(Buffer data, Handler<AsyncResult<Void>> handler) {
            received.appendBuffer(data);
            handler.handle(null);
        }

        @Override
        public void end(Handler<AsyncResult<Void>> handler) {
            assertTrue(handlerCalled.get(), "Response handler should have been called before first bytes are received");
            if (received.length() == fileSize)
                ctx.completeNow();
            handler.handle(null);
        }

        @Override
        public WriteStream<Buffer> setWriteQueueMaxSize(int maxSize) {
            return null;
        }

        @Override
        public boolean writeQueueFull() {
            return false;
        }

        @Override
        public WriteStream<Buffer> drainHandler(@Nullable Handler<Void> handler) {
            return null;
        }
    });
    transformer.setResponseHandler(resp -> {
        handlerCalled.set(true);
    });
    single(s3.getObject(VertxS3ClientSpec::downloadImgReq, transformer)).subscribe(getRes -> ctx.completeNow(), ctx::failNow);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Context(io.vertx.core.Context) Buffer(io.vertx.core.buffer.Buffer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) WriteStream(io.vertx.core.streams.WriteStream) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxAsyncResponseTransformer(io.reactiverse.awssdk.converters.VertxAsyncResponseTransformer) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 28 with S3AsyncClient

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

the class VertxS3ClientSpec method listObjectsV2.

@Test
@Order(8)
void listObjectsV2(Vertx vertx, VertxTestContext ctx) throws Exception {
    final Context originalContext = vertx.getOrCreateContext();
    final S3AsyncClient s3 = s3(originalContext);
    single(s3.putObject(b -> putObjectReq(b, "obj1"), AsyncRequestBody.fromString("hello"))).flatMap(putObjectResponse1 -> single(s3.putObject(b -> putObjectReq(b, "obj2"), AsyncRequestBody.fromString("hi")))).flatMap(putObjectResponse2 -> single(s3.listObjectsV2(VertxS3ClientSpec::listObjectsV2Req)).flatMap(listObjectsV2Response1 -> single(s3.listObjectsV2(b -> listObjectsV2ReqWithContToken(b, listObjectsV2Response1.nextContinuationToken()))).map(listObjectsV2Response2 -> {
        List<S3Object> allObjects = new ArrayList<>(listObjectsV2Response1.contents());
        allObjects.addAll(listObjectsV2Response2.contents());
        return allObjects;
    }))).subscribe(allObjects -> {
        ctx.verify(() -> {
            assertEquals(3, allObjects.size());
            ctx.completeNow();
        });
    }, ctx::failNow);
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Context(io.vertx.core.Context) VertxTestContext(io.vertx.junit5.VertxTestContext) Bucket(software.amazon.awssdk.services.s3.model.Bucket) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) S3Object(software.amazon.awssdk.services.s3.model.S3Object) LocalStackBaseSpec(io.reactiverse.awssdk.integration.LocalStackBaseSpec) VertxAsyncResponseTransformer(io.reactiverse.awssdk.converters.VertxAsyncResponseTransformer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Order(org.junit.jupiter.api.Order) Single(io.reactivex.Single) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) Mimetype(software.amazon.awssdk.core.internal.util.Mimetype) ArrayList(java.util.ArrayList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) WriteStream(io.vertx.core.streams.WriteStream) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) Pump(io.vertx.core.streams.Pump) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) EnabledIfSystemProperty(org.junit.jupiter.api.condition.EnabledIfSystemProperty) AsyncResponseTransformer(software.amazon.awssdk.core.async.AsyncResponseTransformer) AsyncResult(io.vertx.core.AsyncResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MessageProducer(io.vertx.core.eventbus.MessageProducer) ReadStreamPublisher(io.reactiverse.awssdk.reactivestreams.ReadStreamPublisher) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) OpenOptions(io.vertx.core.file.OpenOptions) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Vertx(io.vertx.core.Vertx) LocalstackDockerExtension(cloud.localstack.docker.LocalstackDockerExtension) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) MethodOrderer(org.junit.jupiter.api.MethodOrderer) Nullable(io.vertx.codegen.annotations.Nullable) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) AsyncFile(io.vertx.reactivex.core.file.AsyncFile) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) LocalstackDockerProperties(cloud.localstack.docker.annotation.LocalstackDockerProperties) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Handler(io.vertx.core.Handler) ArrayList(java.util.ArrayList) List(java.util.List) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 29 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 30 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)

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