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/UploadObjectKMSKey.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.ByteArrayOutputStream;
5
import java.io.IOException;
6
7
import com.amazonaws.AmazonServiceException;
8
import com.amazonaws.SdkClientException;
9
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
10
import com.amazonaws.regions.RegionUtils;
11
import com.amazonaws.services.kms.AWSKMS;
12
import com.amazonaws.services.kms.AWSKMSClientBuilder;
13
import com.amazonaws.services.kms.model.CreateKeyResult;
14
import com.amazonaws.services.s3.AmazonS3Encryption;
15
import com.amazonaws.services.s3.AmazonS3EncryptionClientBuilder;
16
import com.amazonaws.services.s3.model.CryptoConfiguration;
17
import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider;
18
import com.amazonaws.services.s3.model.S3Object;
19
import com.amazonaws.services.s3.model.S3ObjectInputStream;
20
21
public class UploadObjectKMSKey {
22
23
public static void main(String[] args) throws IOException {
24
String bucketName = "*** Bucket name ***";
25
String keyName = "*** Object key name ***";
26
String clientRegion = "*** Client region ***";
27
String kms_cmk_id = "***AWS KMS customer master key ID***";
28
int readChunkSize = 4096;
29
30
try {
31
// Optional: If you don't have a KMS key (or need another one),
32
// create one. This example creates a key with AWS-created
33
// key material.
34
AWSKMS kmsClient = AWSKMSClientBuilder.standard()
35
.withCredentials(new ProfileCredentialsProvider())
36
.withRegion(clientRegion)
37
.build();
38
CreateKeyResult keyResult = kmsClient.createKey();
39
kms_cmk_id = keyResult.getKeyMetadata().getKeyId();
40
41
// Create the encryption client.
42
KMSEncryptionMaterialsProvider materialProvider = new KMSEncryptionMaterialsProvider(kms_cmk_id);
43
CryptoConfiguration cryptoConfig = new CryptoConfiguration()
44
.withAwsKmsRegion(RegionUtils.getRegion(clientRegion));
45
AmazonS3Encryption encryptionClient = AmazonS3EncryptionClientBuilder.standard()
46
.withCredentials(new ProfileCredentialsProvider())
47
.withEncryptionMaterials(materialProvider)
48
.withCryptoConfiguration(cryptoConfig)
49
.withRegion(clientRegion).build();
50
51
// Upload an object using the encryption client.
52
String origContent = "S3 Encrypted Object Using KMS-Managed Customer Master Key.";
53
int origContentLength = origContent.length();
54
encryptionClient.putObject(bucketName, keyName, origContent);
55
56
// Download the object. The downloaded object is still encrypted.
57
S3Object downloadedObject = encryptionClient.getObject(bucketName, keyName);
58
S3ObjectInputStream input = downloadedObject.getObjectContent();
59
60
// Decrypt and read the object and close the input stream.
61
byte[] readBuffer = new byte[readChunkSize];
62
ByteArrayOutputStream baos = new ByteArrayOutputStream(readChunkSize);
63
int bytesRead = 0;
64
int decryptedContentLength = 0;
65
66
while ((bytesRead = input.read(readBuffer)) != -1) {
67
baos.write(readBuffer, 0, bytesRead);
68
decryptedContentLength += bytesRead;
69
}
70
input.close();
71
72
// Verify that the original and decrypted contents are the same size.
73
System.out.println("Original content length: " + origContentLength);
74
System.out.println("Decrypted content length: " + decryptedContentLength);
75
}
76
catch(AmazonServiceException e) {
77
// The call was transmitted successfully, but Amazon S3 couldn't process
78
// it, so it returned an error response.
79
e.printStackTrace();
80
}
81
catch(SdkClientException e) {
82
// Amazon S3 couldn't be contacted for a response, or the client
83
// couldn't parse the response from Amazon S3.
84
e.printStackTrace();
85
}
86
}
87
}
88