Search in sources :

Example 1 with GetObjectPresignRequest

use of software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest in project aws-doc-sdk-examples by awsdocs.

the class GetObjectPresignedUrl method getPresignedUrl.

// snippet-start:[presigned.java2.getobjectpresigned.main]
public static void getPresignedUrl(S3Presigner presigner, String bucketName, String keyName) {
    try {
        GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(keyName).build();
        GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder().signatureDuration(Duration.ofMinutes(10)).getObjectRequest(getObjectRequest).build();
        // Generate the presigned request
        PresignedGetObjectRequest presignedGetObjectRequest = presigner.presignGetObject(getObjectPresignRequest);
        // Log the presigned URL
        System.out.println("Presigned URL: " + presignedGetObjectRequest.url());
        HttpURLConnection connection = (HttpURLConnection) presignedGetObjectRequest.url().openConnection();
        presignedGetObjectRequest.httpRequest().headers().forEach((header, values) -> {
            values.forEach(value -> {
                connection.addRequestProperty(header, value);
            });
        });
        // Send any request payload that the service needs (not needed when isBrowserExecutable is true)
        if (presignedGetObjectRequest.signedPayload().isPresent()) {
            connection.setDoOutput(true);
            try (InputStream signedPayload = presignedGetObjectRequest.signedPayload().get().asInputStream();
                OutputStream httpOutputStream = connection.getOutputStream()) {
                IoUtils.copy(signedPayload, httpOutputStream);
            }
        }
        // Download the result of executing the request
        try (InputStream content = connection.getInputStream()) {
            System.out.println("Service returned response: ");
            IoUtils.copy(content, System.out);
        }
    } catch (S3Exception e) {
        e.getStackTrace();
    } catch (IOException e) {
        e.getStackTrace();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) OutputStream(java.io.OutputStream) IOException(java.io.IOException) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) GetObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1 GetObjectPresignRequest (software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest)1 PresignedGetObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest)1