Path: blob/master/code_examples/java_examples/S3Examples/GeneratePresignedUrlAndUploadObject.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;4import java.io.OutputStreamWriter;5import java.net.HttpURLConnection;6import java.net.URL;78import com.amazonaws.AmazonServiceException;9import com.amazonaws.HttpMethod;10import com.amazonaws.SdkClientException;11import com.amazonaws.auth.profile.ProfileCredentialsProvider;12import com.amazonaws.services.s3.AmazonS3;13import com.amazonaws.services.s3.AmazonS3ClientBuilder;14import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;15import com.amazonaws.services.s3.model.S3Object;1617public class GeneratePresignedUrlAndUploadObject {1819public static void main(String[] args) throws IOException {20String clientRegion = "*** Client region ***";21String bucketName = "*** Bucket name ***";22String objectKey = "*** Object key ***";2324try {25AmazonS3 s3Client = AmazonS3ClientBuilder.standard()26.withCredentials(new ProfileCredentialsProvider())27.withRegion(clientRegion)28.build();2930// Set the pre-signed URL to expire after one hour.31java.util.Date expiration = new java.util.Date();32long expTimeMillis = expiration.getTime();33expTimeMillis += 1000 * 60 * 60;34expiration.setTime(expTimeMillis);3536// Generate the pre-signed URL.37System.out.println("Generating pre-signed URL.");38GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey)39.withMethod(HttpMethod.PUT)40.withExpiration(expiration);41URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);4243// Create the connection and use it to upload the new object using the pre-signed URL.44HttpURLConnection connection = (HttpURLConnection) url.openConnection();45connection.setDoOutput(true);46connection.setRequestMethod("PUT");47OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());48out.write("This text uploaded as an object via presigned URL.");49out.close();5051// Check the HTTP response code. To complete the upload and make the object available,52// you must interact with the connection object in some way.53connection.getResponseCode();54System.out.println("HTTP response code: " + connection.getResponseCode());5556// Check to make sure that the object was uploaded successfully.57S3Object object = s3Client.getObject(bucketName, objectKey);58System.out.println("Object " + object.getKey() + " created in bucket " + object.getBucketName());59}60catch(AmazonServiceException e) {61// The call was transmitted successfully, but Amazon S3 couldn't process62// it, so it returned an error response.63e.printStackTrace();64}65catch(SdkClientException e) {66// Amazon S3 couldn't be contacted for a response, or the client67// couldn't parse the response from Amazon S3.68e.printStackTrace();69}70}71}7273