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