Path: blob/master/code_examples/java_examples/S3Examples/ServerSideEncryptionUsingClientSideEncryptionKey.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.BufferedReader;4import java.io.File;5import java.io.IOException;6import java.io.InputStreamReader;7import java.security.NoSuchAlgorithmException;8import java.security.SecureRandom;910import javax.crypto.KeyGenerator;1112import com.amazonaws.AmazonServiceException;13import com.amazonaws.SdkClientException;14import com.amazonaws.auth.profile.ProfileCredentialsProvider;15import com.amazonaws.services.s3.AmazonS3;16import com.amazonaws.services.s3.AmazonS3ClientBuilder;17import com.amazonaws.services.s3.model.CopyObjectRequest;18import com.amazonaws.services.s3.model.GetObjectMetadataRequest;19import com.amazonaws.services.s3.model.GetObjectRequest;20import com.amazonaws.services.s3.model.ObjectMetadata;21import com.amazonaws.services.s3.model.PutObjectRequest;22import com.amazonaws.services.s3.model.S3Object;23import com.amazonaws.services.s3.model.S3ObjectInputStream;24import com.amazonaws.services.s3.model.SSECustomerKey;2526public class ServerSideEncryptionUsingClientSideEncryptionKey {27private static SSECustomerKey SSE_KEY;28private static AmazonS3 S3_CLIENT;29private static KeyGenerator KEY_GENERATOR;3031public static void main(String[] args) throws IOException, NoSuchAlgorithmException {32String clientRegion = "*** Client region ***";33String bucketName = "*** Bucket name ***";34String keyName = "*** Key name ***";35String uploadFileName = "*** File path ***";36String targetKeyName = "*** Target key name ***";3738// Create an encryption key.39KEY_GENERATOR = KeyGenerator.getInstance("AES");40KEY_GENERATOR.init(256, new SecureRandom());41SSE_KEY = new SSECustomerKey(KEY_GENERATOR.generateKey());4243try {44S3_CLIENT = AmazonS3ClientBuilder.standard()45.withCredentials(new ProfileCredentialsProvider())46.withRegion(clientRegion)47.build();4849// Upload an object.50uploadObject(bucketName, keyName, new File(uploadFileName));5152// Download the object.53downloadObject(bucketName, keyName);5455// Verify that the object is properly encrypted by attempting to retrieve it56// using the encryption key.57retrieveObjectMetadata(bucketName, keyName);5859// Copy the object into a new object that also uses SSE-C.60copyObject(bucketName, keyName, targetKeyName);61}62catch(AmazonServiceException e) {63// The call was transmitted successfully, but Amazon S3 couldn't process64// it, so it returned an error response.65e.printStackTrace();66}67catch(SdkClientException e) {68// Amazon S3 couldn't be contacted for a response, or the client69// couldn't parse the response from Amazon S3.70e.printStackTrace();71}72}7374private static void uploadObject(String bucketName, String keyName, File file) {75PutObjectRequest putRequest = new PutObjectRequest(bucketName, keyName, file).withSSECustomerKey(SSE_KEY);76S3_CLIENT.putObject(putRequest);77System.out.println("Object uploaded");78}7980private static void downloadObject(String bucketName, String keyName) throws IOException {81GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, keyName).withSSECustomerKey(SSE_KEY);82S3Object object = S3_CLIENT.getObject(getObjectRequest);8384System.out.println("Object content: ");85displayTextInputStream(object.getObjectContent());86}8788private static void retrieveObjectMetadata(String bucketName, String keyName) {89GetObjectMetadataRequest getMetadataRequest = new GetObjectMetadataRequest(bucketName, keyName)90.withSSECustomerKey(SSE_KEY);91ObjectMetadata objectMetadata = S3_CLIENT.getObjectMetadata(getMetadataRequest);92System.out.println("Metadata retrieved. Object size: " + objectMetadata.getContentLength());93}9495private static void copyObject(String bucketName, String keyName, String targetKeyName)96throws NoSuchAlgorithmException {97// Create a new encryption key for target so that the target is saved using SSE-C.98SSECustomerKey newSSEKey = new SSECustomerKey(KEY_GENERATOR.generateKey());99100CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, keyName, bucketName, targetKeyName)101.withSourceSSECustomerKey(SSE_KEY)102.withDestinationSSECustomerKey(newSSEKey);103104S3_CLIENT.copyObject(copyRequest);105System.out.println("Object copied");106}107108private static void displayTextInputStream(S3ObjectInputStream input) throws IOException {109// Read one line at a time from the input stream and display each line.110BufferedReader reader = new BufferedReader(new InputStreamReader(input));111String line;112while ((line = reader.readLine()) != null) {113System.out.println(line);114}115System.out.println();116}117}118119120