xxxxxxxxxx
public static void signBucket(S3Presigner presigner, String bucketName, String keyName) {
try {
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.
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 | IOException e) {
e.getStackTrace();
}
}
https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html