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/ServerSideEncryptionCopyObjectUsingHLwithSSEC.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.File;
5
import java.security.SecureRandom;
6
7
import javax.crypto.KeyGenerator;
8
9
import com.amazonaws.AmazonServiceException;
10
import com.amazonaws.SdkClientException;
11
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
12
import com.amazonaws.services.s3.AmazonS3;
13
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
14
import com.amazonaws.services.s3.model.CopyObjectRequest;
15
import com.amazonaws.services.s3.model.PutObjectRequest;
16
import com.amazonaws.services.s3.model.SSECustomerKey;
17
import com.amazonaws.services.s3.transfer.Copy;
18
import com.amazonaws.services.s3.transfer.TransferManager;
19
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
20
import com.amazonaws.services.s3.transfer.Upload;
21
22
public class ServerSideEncryptionCopyObjectUsingHLwithSSEC {
23
24
public static void main(String[] args) throws Exception {
25
String clientRegion = "*** Client region ***";
26
String bucketName = "*** Bucket name ***";
27
String fileToUpload = "*** File path ***";
28
String keyName = "*** New object key name ***";
29
String targetKeyName = "*** Key name for object copy ***";
30
31
try {
32
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
33
.withRegion(clientRegion)
34
.withCredentials(new ProfileCredentialsProvider())
35
.build();
36
TransferManager tm = TransferManagerBuilder.standard()
37
.withS3Client(s3Client)
38
.build();
39
40
// Create an object from a file.
41
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, new File(fileToUpload));
42
43
// Create an encryption key.
44
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
45
keyGenerator.init(256, new SecureRandom());
46
SSECustomerKey sseCustomerEncryptionKey = new SSECustomerKey(keyGenerator.generateKey());
47
48
// Upload the object. TransferManager uploads asynchronously, so this call returns immediately.
49
putObjectRequest.setSSECustomerKey(sseCustomerEncryptionKey);
50
Upload upload = tm.upload(putObjectRequest);
51
52
// Optionally, wait for the upload to finish before continuing.
53
upload.waitForCompletion();
54
System.out.println("Object created.");
55
56
// Copy the object and store the copy using SSE-C with a new key.
57
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, keyName, bucketName, targetKeyName);
58
SSECustomerKey sseTargetObjectEncryptionKey = new SSECustomerKey(keyGenerator.generateKey());
59
copyObjectRequest.setSourceSSECustomerKey(sseCustomerEncryptionKey);
60
copyObjectRequest.setDestinationSSECustomerKey(sseTargetObjectEncryptionKey);
61
62
// Copy the object. TransferManager copies asynchronously, so this call returns immediately.
63
Copy copy = tm.copy(copyObjectRequest);
64
65
// Optionally, wait for the upload to finish before continuing.
66
copy.waitForCompletion();
67
System.out.println("Copy complete.");
68
}
69
catch(AmazonServiceException e) {
70
// The call was transmitted successfully, but Amazon S3 couldn't process
71
// it, so it returned an error response.
72
e.printStackTrace();
73
}
74
catch(SdkClientException e) {
75
// Amazon S3 couldn't be contacted for a response, or the client
76
// couldn't parse the response from Amazon S3.
77
e.printStackTrace();
78
}
79
}
80
}
81