Search in sources :

Example 1 with PresignedUploadPartRequest

use of software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest in project formkiq-core by formkiq.

the class S3Service method presignPostUrl.

/**
 * Generate a S3 Signed Url for creating an object using POST request.
 *
 * @param bucket {@link String}
 * @param key {@link String}
 * @param duration {@link Duration}
 * @param contentLength {@link Optional} {@link Long}
 * @return {@link URL}
 */
public URL presignPostUrl(final String bucket, final String key, final Duration duration, final Optional<Long> contentLength) {
    try (S3Presigner signer = this.builder.buildPresigner()) {
        UploadPartRequest.Builder uploadBuilder = UploadPartRequest.builder().bucket(bucket).key(key);
        if (contentLength.isPresent()) {
            uploadBuilder = uploadBuilder.contentLength(contentLength.get());
        }
        UploadPartPresignRequest prereq = UploadPartPresignRequest.builder().signatureDuration(duration).uploadPartRequest(uploadBuilder.build()).build();
        PresignedUploadPartRequest req = signer.presignUploadPart(prereq);
        return req.url();
    }
}
Also used : UploadPartPresignRequest(software.amazon.awssdk.services.s3.presigner.model.UploadPartPresignRequest) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest)

Example 2 with PresignedUploadPartRequest

use of software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest in project aws-sdk-java-v2 by aws.

the class S3PresignerIntegrationTest method uploadPart_CanBePresigned.

@Test
public void uploadPart_CanBePresigned() throws IOException {
    String objectKey = generateRandomObjectKey();
    S3TestUtils.addCleanupTask(S3PresignerIntegrationTest.class, () -> client.deleteObject(r -> r.bucket(testBucket).key(objectKey)));
    CreateMultipartUploadResponse create = client.createMultipartUpload(createMultipartUploadRequest(objectKey));
    S3TestUtils.addCleanupTask(S3PresignerIntegrationTest.class, () -> client.abortMultipartUpload(abortMultipartUploadRequest(objectKey, create.uploadId())));
    PresignedUploadPartRequest uploadPart = presigner.presignUploadPart(up -> up.signatureDuration(Duration.ofDays(1)).uploadPartRequest(upr -> upr.bucket(testBucket).key(objectKey).partNumber(1).uploadId(create.uploadId())));
    HttpExecuteResponse uploadPartResponse = execute(uploadPart, testObjectContent);
    assertThat(uploadPartResponse.httpResponse().isSuccessful()).isTrue();
    String etag = uploadPartResponse.httpResponse().firstMatchingHeader("ETag").orElse(null);
    client.completeMultipartUpload(createMultipartUploadRequest(objectKey, create, etag));
    String content = client.getObjectAsBytes(r -> r.bucket(testBucket).key(objectKey)).asUtf8String();
    assertThat(content).isEqualTo(testObjectContent);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeClass(org.junit.BeforeClass) ApacheHttpClient(software.amazon.awssdk.http.apache.ApacheHttpClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) PresignedAbortMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedAbortMultipartUploadRequest) RequestPayer(software.amazon.awssdk.services.s3.model.RequestPayer) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Duration(java.time.Duration) After(org.junit.After) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) Before(org.junit.Before) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) AfterClass(org.junit.AfterClass) MultipartUpload(software.amazon.awssdk.services.s3.model.MultipartUpload) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) IOException(java.io.IOException) Test(org.junit.Test) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) UUID(java.util.UUID) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) PresignedCompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCompleteMultipartUploadRequest) S3TestUtils(software.amazon.awssdk.services.s3.utils.S3TestUtils) Consumer(java.util.function.Consumer) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) StringInputStream(software.amazon.awssdk.utils.StringInputStream) PresignedCreateMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCreateMultipartUploadRequest) Optional(java.util.Optional) RequestBody(software.amazon.awssdk.core.sync.RequestBody) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) IoUtils(software.amazon.awssdk.utils.IoUtils) InputStream(java.io.InputStream) HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) UploadPartResponse(software.amazon.awssdk.services.s3.model.UploadPartResponse) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) Test(org.junit.Test)

Aggregations

UploadPartRequest (software.amazon.awssdk.services.s3.model.UploadPartRequest)2 S3Presigner (software.amazon.awssdk.services.s3.presigner.S3Presigner)2 PresignedUploadPartRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 Duration (java.time.Duration)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Consumer (java.util.function.Consumer)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 After (org.junit.After)1 AfterClass (org.junit.AfterClass)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1 PresignedRequest (software.amazon.awssdk.awscore.presigner.PresignedRequest)1 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 RequestBody (software.amazon.awssdk.core.sync.RequestBody)1 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)1