Search in sources :

Example 1 with PutObjectPresignRequest

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

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

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

the class GeneratePresignedUrlUploadImage method signBucket.

// snippet-start:[presigned.java2.generatepresignedurlimage.main]
public static void signBucket(S3Presigner presigner, String bucketName, String keyName, byte[] pic) {
    try {
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(keyName).contentType("image/png").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", "image/png");
        connection.setRequestMethod("PUT");
        connection.getOutputStream().write(pic);
        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) 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)

Aggregations

IOException (java.io.IOException)3 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)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 HashMap (java.util.HashMap)1