Search in sources :

Example 1 with S3Presigner

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

the class GeneratePresignedUrlMetadata method signBucket.

// snippet-start:[presigned.java2.generatepresignedurl.metadata.main]
public static void signBucket(S3Presigner presigner, String bucketName, String keyName) {
    try {
        // set metadata
        Map<String, String> metadata = new HashMap<>();
        metadata.put("author", "Mary Doe");
        metadata.put("version", "1.0.0.0");
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(keyName).contentType("text/plain").metadata(metadata).build();
        PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder().signatureDuration(Duration.ofMinutes(10)).putObjectRequest(objectRequest).build();
        PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest);
        System.out.println("Presigned URL to upload a file to: " + presignedRequest.url());
        System.out.println("Which HTTP method needs to be used when uploading a file: " + presignedRequest.httpRequest().method());
        // Upload content to the Amazon S3 bucket by using this URL
        URL url = presignedRequest.url();
        // Create the connection and use it to upload the new object
        // Notice we specify metadata as well
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("x-amz-meta-author", "Mary Doe");
        connection.setRequestProperty("x-amz-meta-version", "1.0.0.0");
        connection.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("This text was uploaded as an object by using a presigned URL.");
        out.close();
        connection.getResponseCode();
        System.out.println("HTTP response code is " + connection.getResponseCode());
    } catch (S3Exception e) {
        e.getStackTrace();
    } catch (IOException e) {
        e.getStackTrace();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) URL(java.net.URL)

Example 2 with S3Presigner

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

the class GeneratePresignedUrlAndUploadObject method signBucket.

// snippet-start:[presigned.java2.generatepresignedurl.main]
public static void signBucket(S3Presigner presigner, String bucketName, String keyName) {
    try {
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(keyName).contentType("text/plain").build();
        PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder().signatureDuration(Duration.ofMinutes(10)).putObjectRequest(objectRequest).build();
        PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest);
        String myURL = presignedRequest.url().toString();
        System.out.println("Presigned URL to upload a file to: " + myURL);
        System.out.println("Which HTTP method needs to be used when uploading a file: " + presignedRequest.httpRequest().method());
        // Upload content to the Amazon S3 bucket by using this URL
        URL url = presignedRequest.url();
        // Create the connection and use it to upload the new object by using the presigned URL
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("This text was uploaded as an object by using a presigned URL.");
        out.close();
        connection.getResponseCode();
        System.out.println("HTTP response code is " + connection.getResponseCode());
    } catch (S3Exception e) {
        e.getStackTrace();
    } catch (IOException e) {
        e.getStackTrace();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) URL(java.net.URL)

Example 3 with S3Presigner

use of software.amazon.awssdk.services.s3.presigner.S3Presigner 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)

Example 4 with S3Presigner

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

the class GetObjectPresignedUrl method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetObjectPresignedUrl <bucketName> <keyName> \n\n" + "Where:\n" + "    bucketName - the Amazon S3 bucket name. \n\n" + "    keyName - a key name that represents a text file. \n\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String keyName = args[1];
    Region region = Region.US_WEST_2;
    S3Presigner presigner = S3Presigner.builder().region(region).build();
    getPresignedUrl(presigner, bucketName, keyName);
    presigner.close();
}
Also used : S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Region(software.amazon.awssdk.regions.Region)

Example 5 with S3Presigner

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

the class GeneratePresignedUrlMetadata method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GeneratePresignedUrlAndUploadObject <bucketName> <keyName> \n\n" + "Where:\n" + "    bucketName - the name of the Amazon S3 bucket. \n\n" + "    keyName - a key name that represents a text file. \n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String keyName = args[1];
    Region region = Region.US_EAST_1;
    S3Presigner presigner = S3Presigner.builder().region(region).build();
    signBucket(presigner, bucketName, keyName);
    presigner.close();
}
Also used : S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Region(software.amazon.awssdk.regions.Region)

Aggregations

IOException (java.io.IOException)4 HttpURLConnection (java.net.HttpURLConnection)4 Region (software.amazon.awssdk.regions.Region)4 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)4 S3Presigner (software.amazon.awssdk.services.s3.presigner.S3Presigner)4 URL (java.net.URL)3 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)3 PresignedPutObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest)3 PutObjectPresignRequest (software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest)3 OutputStreamWriter (java.io.OutputStreamWriter)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)1 GetObjectPresignRequest (software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest)1 PresignedGetObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest)1