use of software.amazon.awssdk.awscore.presigner.PresignedRequest in project aws-sdk-java-v2 by aws.
the class S3PresignerIntegrationTest method abortMultipartUpload_CanBePresigned.
@Test
public void abortMultipartUpload_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())));
PresignedAbortMultipartUploadRequest presignedRequest = presigner.presignAbortMultipartUpload(r -> r.signatureDuration(Duration.ofDays(1)).abortMultipartUploadRequest(abortMultipartUploadRequest(objectKey, create.uploadId())));
assertThat(execute(presignedRequest, null).httpResponse().isSuccessful()).isTrue();
assertThat(getMultipartUpload(objectKey)).isNotPresent();
}
use of software.amazon.awssdk.awscore.presigner.PresignedRequest in project aws-sdk-java-v2 by aws.
the class DefaultS3Presigner method initializePresignedRequest.
/**
* Initialize the provided presigned request.
*/
private void initializePresignedRequest(PresignedRequest.Builder presignedRequest, ExecutionContext execCtx, SdkHttpFullRequest signedHttpRequest) {
SdkBytes signedPayload = signedHttpRequest.contentStreamProvider().map(p -> SdkBytes.fromInputStream(p.newStream())).orElse(null);
List<String> signedHeadersQueryParam = signedHttpRequest.rawQueryParameters().get("X-Amz-SignedHeaders");
Validate.validState(signedHeadersQueryParam != null, "Only SigV4 presigners are supported at this time, but the configured " + "presigner (%s) did not seem to generate a SigV4 signature.", execCtx.signer());
Map<String, List<String>> signedHeaders = signedHeadersQueryParam.stream().flatMap(h -> Stream.of(h.split(";"))).collect(toMap(h -> h, h -> signedHttpRequest.firstMatchingHeader(h).map(Collections::singletonList).orElseGet(ArrayList::new)));
boolean isBrowserExecutable = signedHttpRequest.method() == SdkHttpMethod.GET && signedPayload == null && (signedHeaders.isEmpty() || (signedHeaders.size() == 1 && signedHeaders.containsKey("host")));
presignedRequest.expiration(execCtx.executionAttributes().getAttribute(PRESIGNER_EXPIRATION)).isBrowserExecutable(isBrowserExecutable).httpRequest(signedHttpRequest).signedHeaders(signedHeaders).signedPayload(signedPayload);
}
use of software.amazon.awssdk.awscore.presigner.PresignedRequest in project aws-sdk-java-v2 by aws.
the class S3MrapIntegrationTest method applyPresignedUrl.
private String applyPresignedUrl(PresignedRequest presignedRequest, String content) {
try {
HttpExecuteRequest.Builder builder = HttpExecuteRequest.builder().request(presignedRequest.httpRequest());
if (!isEmpty(content)) {
builder.contentStreamProvider(() -> new StringInputStream(content));
}
HttpExecuteRequest request = builder.build();
HttpExecuteResponse response = ApacheHttpClient.create().prepareRequest(request).call();
return response.responseBody().map(stream -> invokeSafely(() -> IoUtils.toUtf8String(stream))).orElseThrow(() -> new IOException("No input stream"));
} catch (IOException e) {
log.error(() -> "Error occurred ", e);
}
return null;
}
use of software.amazon.awssdk.awscore.presigner.PresignedRequest in project aws-sdk-java-v2 by aws.
the class S3PresignerIntegrationTest method completeMultipartUpload_CanBePresigned.
@Test
public void completeMultipartUpload_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())));
UploadPartResponse uploadPartResponse = client.uploadPart(uploadPartRequest(objectKey, create), RequestBody.fromString(testObjectContent));
String etag = uploadPartResponse.eTag();
PresignedCompleteMultipartUploadRequest presignedRequest = presigner.presignCompleteMultipartUpload(r -> r.signatureDuration(Duration.ofDays(1)).completeMultipartUploadRequest(createMultipartUploadRequest(objectKey, create, etag)));
assertThat(execute(presignedRequest, presignedRequest.signedPayload().get().asUtf8String()).httpResponse().isSuccessful()).isTrue();
String content = client.getObjectAsBytes(r -> r.bucket(testBucket).key(objectKey)).asUtf8String();
assertThat(content).isEqualTo(testObjectContent);
}
Aggregations