Search in sources :

Example 1 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class DefaultS3TransferManager method uploadFile.

@Override
public FileUpload uploadFile(UploadFileRequest uploadFileRequest) {
    Validate.paramNotNull(uploadFileRequest, "uploadFileRequest");
    AsyncRequestBody requestBody = AsyncRequestBody.fromFile(uploadFileRequest.source());
    CompletableFuture<CompletedFileUpload> uploadFuture = new CompletableFuture<>();
    TransferProgressUpdater progressUpdater = new TransferProgressUpdater(uploadFileRequest, requestBody);
    progressUpdater.transferInitiated();
    requestBody = progressUpdater.wrapRequestBody(requestBody);
    progressUpdater.registerCompletion(uploadFuture);
    try {
        assertNotUnsupportedArn(uploadFileRequest.putObjectRequest().bucket(), "upload");
        CompletableFuture<PutObjectResponse> putObjFuture = s3CrtAsyncClient.putObject(uploadFileRequest.putObjectRequest(), requestBody);
        // Forward upload cancellation to CRT future
        CompletableFutureUtils.forwardExceptionTo(uploadFuture, putObjFuture);
        CompletableFutureUtils.forwardTransformedResultTo(putObjFuture, uploadFuture, r -> CompletedFileUpload.builder().response(r).build());
    } catch (Throwable throwable) {
        uploadFuture.completeExceptionally(throwable);
    }
    return new DefaultFileUpload(uploadFuture, progressUpdater.progress());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) TransferProgressUpdater(software.amazon.awssdk.transfer.s3.internal.progress.TransferProgressUpdater) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody)

Example 2 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class DefaultS3TransferManager method upload.

@Override
public Upload upload(UploadRequest uploadRequest) {
    Validate.paramNotNull(uploadRequest, "uploadRequest");
    AsyncRequestBody requestBody = uploadRequest.requestBody();
    CompletableFuture<CompletedUpload> uploadFuture = new CompletableFuture<>();
    TransferProgressUpdater progressUpdater = new TransferProgressUpdater(uploadRequest, requestBody);
    progressUpdater.transferInitiated();
    requestBody = progressUpdater.wrapRequestBody(requestBody);
    progressUpdater.registerCompletion(uploadFuture);
    try {
        assertNotUnsupportedArn(uploadRequest.putObjectRequest().bucket(), "upload");
        CompletableFuture<PutObjectResponse> putObjFuture = s3CrtAsyncClient.putObject(uploadRequest.putObjectRequest(), requestBody);
        // Forward upload cancellation to CRT future
        CompletableFutureUtils.forwardExceptionTo(uploadFuture, putObjFuture);
        CompletableFutureUtils.forwardTransformedResultTo(putObjFuture, uploadFuture, r -> CompletedUpload.builder().response(r).build());
    } catch (Throwable throwable) {
        uploadFuture.completeExceptionally(throwable);
    }
    return new DefaultUpload(uploadFuture, progressUpdater.progress());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CompletedUpload(software.amazon.awssdk.transfer.s3.CompletedUpload) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) TransferProgressUpdater(software.amazon.awssdk.transfer.s3.internal.progress.TransferProgressUpdater) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody)

Example 3 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class S3CrtClientPutObjectIntegrationTest method putObject_byteBufferBody_objectSentCorrectly.

@Test
public void putObject_byteBufferBody_objectSentCorrectly() {
    byte[] data = new byte[16384];
    new Random().nextBytes(data);
    ByteBuffer byteBuffer = ByteBuffer.wrap(data);
    AsyncRequestBody body = AsyncRequestBody.fromByteBuffer(byteBuffer);
    s3Crt.putObject(r -> r.bucket(TEST_BUCKET).key(TEST_KEY), body).join();
    ResponseBytes<GetObjectResponse> responseBytes = S3IntegrationTestBase.s3.getObject(r -> r.bucket(TEST_BUCKET).key(TEST_KEY), ResponseTransformer.toBytes());
    byte[] expectedSum = ChecksumUtils.computeCheckSum(byteBuffer);
    Assertions.assertThat(ChecksumUtils.computeCheckSum(responseBytes.asByteBuffer())).isEqualTo(expectedSum);
}
Also used : AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) Files(java.nio.file.Files) S3BucketUtils.temporaryBucketName(software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName) IOException(java.io.IOException) Random(java.util.Random) Test(org.junit.Test) ResponseBytes(software.amazon.awssdk.core.ResponseBytes) ResponseTransformer(software.amazon.awssdk.core.sync.ResponseTransformer) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) List(java.util.List) Stream(java.util.stream.Stream) Flowable(io.reactivex.Flowable) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) S3CrtAsyncClient(software.amazon.awssdk.transfer.s3.internal.S3CrtAsyncClient) Optional(java.util.Optional) Assertions(org.assertj.core.api.Assertions) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) Subscriber(org.reactivestreams.Subscriber) RandomTempFile(software.amazon.awssdk.testutils.RandomTempFile) ChecksumUtils(software.amazon.awssdk.transfer.s3.util.ChecksumUtils) Random(java.util.Random) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class S3CrtClientPutObjectIntegrationTest method putObject_customRequestBody_objectSentCorrectly.

@Test
public void putObject_customRequestBody_objectSentCorrectly() throws IOException {
    Random rng = new Random();
    int bufferSize = 16384;
    int nBuffers = 15;
    List<ByteBuffer> bodyData = Stream.generate(() -> {
        byte[] data = new byte[bufferSize];
        rng.nextBytes(data);
        return ByteBuffer.wrap(data);
    }).limit(nBuffers).collect(Collectors.toList());
    long contentLength = bufferSize * nBuffers;
    byte[] expectedSum = ChecksumUtils.computeCheckSum(bodyData);
    Flowable<ByteBuffer> publisher = Flowable.fromIterable(bodyData);
    AsyncRequestBody customRequestBody = new AsyncRequestBody() {

        @Override
        public Optional<Long> contentLength() {
            return Optional.of(contentLength);
        }

        @Override
        public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
            publisher.subscribe(subscriber);
        }
    };
    s3Crt.putObject(r -> r.bucket(TEST_BUCKET).key(TEST_KEY), customRequestBody).join();
    ResponseInputStream<GetObjectResponse> objContent = S3IntegrationTestBase.s3.getObject(r -> r.bucket(TEST_BUCKET).key(TEST_KEY), ResponseTransformer.toInputStream());
    Assertions.assertThat(ChecksumUtils.computeCheckSum(objContent)).isEqualTo(expectedSum);
}
Also used : AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) Files(java.nio.file.Files) S3BucketUtils.temporaryBucketName(software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName) IOException(java.io.IOException) Random(java.util.Random) Test(org.junit.Test) ResponseBytes(software.amazon.awssdk.core.ResponseBytes) ResponseTransformer(software.amazon.awssdk.core.sync.ResponseTransformer) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) List(java.util.List) Stream(java.util.stream.Stream) Flowable(io.reactivex.Flowable) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) S3CrtAsyncClient(software.amazon.awssdk.transfer.s3.internal.S3CrtAsyncClient) Optional(java.util.Optional) Assertions(org.assertj.core.api.Assertions) ResponseInputStream(software.amazon.awssdk.core.ResponseInputStream) Subscriber(org.reactivestreams.Subscriber) RandomTempFile(software.amazon.awssdk.testutils.RandomTempFile) ChecksumUtils(software.amazon.awssdk.transfer.s3.util.ChecksumUtils) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) Random(java.util.Random) Subscriber(org.reactivestreams.Subscriber) Test(org.junit.Test)

Example 5 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class Aws4EventStreamSignerTest method generateTestVector.

TestVector generateTestVector() {
    return new TestVector() {

        List<String> requestBody = Lists.newArrayList("A", "B", "C");

        @Override
        public List<String> requestBody() {
            return requestBody;
        }

        @Override
        public SdkHttpFullRequest.Builder httpFullRequest() {
            // Signing key: "29dc0a760fed568677d74136ad02d315a07d31b8f321f5c43350f284dac892c";
            return SdkHttpFullRequest.builder().method(SdkHttpMethod.POST).putHeader("Host", "demo.us-east-1.amazonaws.com").putHeader("content-encoding", "application/vnd.amazon.eventstream").putHeader("x-amz-content-sha256", "STREAMING-AWS4-HMAC-SHA256-EVENTS").encodedPath("/streaming").protocol("https").host("demo.us-east-1.amazonaws.com");
        }

        @Override
        public AsyncRequestBody requestBodyPublisher() {
            List<ByteBuffer> bodyBytes = requestBody.stream().map(s -> ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8))).collect(Collectors.toList());
            Publisher<ByteBuffer> bodyPublisher = Flowable.fromIterable(bodyBytes);
            return AsyncRequestBody.fromPublisher(bodyPublisher);
        }

        @Override
        public Flowable<Message> expectedMessagePublisher() {
            Flowable<String> sigsHex = Flowable.just("7aabf85b765e6a4d0d500b6e968657b14726fa3e1eb7e839302728ffd77629a5", "f72aa9642f571d24a6e1ae42f10f073ad9448d8a028b6bcd82da081335adda02", "632af120435b57ec241d8bfbb12e496dfd5e2730a1a02ac0ab6eaa230ae02e9a", "c6f679ddb3af68f5e82f0cf6761244cb2338cf11e7d01a24130aea1b7c17e53e");
            // The Last data frame is empty
            Flowable<String> payloads = Flowable.fromIterable(requestBody).concatWith(Flowable.just(""));
            return sigsHex.zipWith(payloads, new BiFunction<String, String, Message>() {

                // The first Instant was used to sign the request
                private int idx = 1;

                @Override
                public Message apply(String sig, String payload) throws Exception {
                    Map<String, HeaderValue> headers = new HashMap<>();
                    headers.put(EVENT_STREAM_DATE, HeaderValue.fromTimestamp(SIGNING_INSTANTS.get(idx++)));
                    headers.put(EVENT_STREAM_SIGNATURE, HeaderValue.fromByteArray(BinaryUtils.fromHex(sig)));
                    return new Message(headers, payload.getBytes(StandardCharsets.UTF_8));
                }
            });
        }
    };
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BiFunction(io.reactivex.functions.BiFunction) SignerTestUtils(software.amazon.awssdk.auth.signer.internal.SignerTestUtils) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Message(software.amazon.eventstream.Message) EVENT_STREAM_DATE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_DATE) ArrayList(java.util.ArrayList) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BinaryUtils(software.amazon.awssdk.utils.BinaryUtils) TestSubscriber(io.reactivex.subscribers.TestSubscriber) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) ZoneOffset(java.time.ZoneOffset) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) Mockito.times(org.mockito.Mockito.times) EVENT_STREAM_SIGNATURE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_SIGNATURE) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MessageDecoder(software.amazon.eventstream.MessageDecoder) Mockito.never(org.mockito.Mockito.never) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) Function(io.reactivex.functions.Function) Lists(org.assertj.core.util.Lists) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Subscription(org.reactivestreams.Subscription) Clock(java.time.Clock) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) ByteBuffer(java.nio.ByteBuffer) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)50 ByteBuffer (java.nio.ByteBuffer)25 Test (org.junit.jupiter.api.Test)23 List (java.util.List)16 Test (org.junit.Test)13 Subscriber (org.reactivestreams.Subscriber)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)11 Publisher (org.reactivestreams.Publisher)9 Flowable (io.reactivex.Flowable)8 Consumer (java.util.function.Consumer)8 AsyncAws4Signer (software.amazon.awssdk.auth.signer.AsyncAws4Signer)8 Signer (software.amazon.awssdk.core.signer.Signer)8 MetricCollector (software.amazon.awssdk.metrics.MetricCollector)8 Collections (java.util.Collections)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 Generated (software.amazon.awssdk.annotations.Generated)7 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)7 AwsRequestOverrideConfiguration (software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration)7