GeneratePresignedUrlAndUploadObject.java - AWS Code Sample

GeneratePresignedUrlAndUploadObject.java

GeneratePresignedUrlAndUploadObject.java demonstrates how to use the S3Presigner client object to create a presigned URL and upload an object to a S3 bucket

/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * */ package com.example.s3; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.time.Duration; import software.amazon.awssdk.services.s3.model.ObjectCannedACL; import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest; import software.amazon.awssdk.services.s3.presigner.S3Presigner; import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest; public class GeneratePresignedUrlAndUploadObject { public static void main(String[] args) { if (args.length < 2) { System.out.println("Please specify a bucket name and a key name that represents a text file"); System.exit(1); } String bucketName = args[0]; String keyName = args[1]; // Create a S3Presigner by using the default AWS Region and credentials S3Presigner presigner = S3Presigner.create(); signBucket(presigner, bucketName, keyName); } public static void signBucket(S3Presigner presigner, String bucketName, String keyName) { try { // Use a PutObjectRequest to set additional values 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); System.out.println("Pre-signed 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 bucket by using this URL URL url = presignedRequest.url(); // Create the connection and use it to upload the new object by using the pre-signed 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 uploaded as an object via presigned URL."); out.close(); connection.getResponseCode(); System.out.println("HTTP response code: " + connection.getResponseCode()); /* * It's recommended that you close the S3Presigner when it is done being used, because some credential * providers (e.g. if your AWS profile is configured to assume an STS role) require system resources * that need to be freed. If you are using one S3Presigner per application (as recommended), this * usually isn't needed */ presigner.close(); } catch (S3Exception e) { e.getStackTrace(); } catch (IOException e) { e.getStackTrace(); } } }

Sample Details

Service: S3

Last tested: 2/6/2020

Author: scmacdon-aws

Type: full-example