Path: blob/master/code_examples/java_examples/S3Examples/SpecifyServerSideEncryption.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.ByteArrayInputStream;45import com.amazonaws.AmazonServiceException;6import com.amazonaws.SdkClientException;7import com.amazonaws.auth.profile.ProfileCredentialsProvider;8import com.amazonaws.services.s3.AmazonS3;9import com.amazonaws.services.s3.AmazonS3ClientBuilder;10import com.amazonaws.services.s3.internal.SSEResultBase;11import com.amazonaws.services.s3.model.CopyObjectRequest;12import com.amazonaws.services.s3.model.CopyObjectResult;13import com.amazonaws.services.s3.model.ObjectMetadata;14import com.amazonaws.services.s3.model.PutObjectRequest;15import com.amazonaws.services.s3.model.PutObjectResult;1617public class SpecifyServerSideEncryption {1819public static void main(String[] args) {20String clientRegion = "*** Client region ***";21String bucketName = "*** Bucket name ***";22String keyNameToEncrypt = "*** Key name for an object to upload and encrypt ***";23String keyNameToCopyAndEncrypt = "*** Key name for an unencrypted object to be encrypted by copying ***";24String copiedObjectKeyName = "*** Key name for the encrypted copy of the unencrypted object ***";2526try {27AmazonS3 s3Client = AmazonS3ClientBuilder.standard()28.withRegion(clientRegion)29.withCredentials(new ProfileCredentialsProvider())30.build();3132// Upload an object and encrypt it with SSE.33uploadObjectWithSSEEncryption(s3Client, bucketName, keyNameToEncrypt);3435// Upload a new unencrypted object, then change its encryption state36// to encrypted by making a copy.37changeSSEEncryptionStatusByCopying(s3Client,38bucketName,39keyNameToCopyAndEncrypt,40copiedObjectKeyName);41}42catch(AmazonServiceException e) {43// The call was transmitted successfully, but Amazon S3 couldn't process44// it, so it returned an error response.45e.printStackTrace();46}47catch(SdkClientException e) {48// Amazon S3 couldn't be contacted for a response, or the client49// couldn't parse the response from Amazon S3.50e.printStackTrace();51}52}5354private static void uploadObjectWithSSEEncryption(AmazonS3 s3Client, String bucketName, String keyName) {55String objectContent = "Test object encrypted with SSE";5657// Specify server-side encryption.58ObjectMetadata objectMetadata = new ObjectMetadata();59objectMetadata.setContentLength(objectContent.length());60objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);61PutObjectRequest putRequest = new PutObjectRequest(bucketName,62keyName,63new ByteArrayInputStream(objectContent.getBytes()),64objectMetadata);6566// Upload the object and check its encryption status.67PutObjectResult putResult = s3Client.putObject(putRequest);68System.out.println("Object \"" + keyName + "\" uploaded with SSE.");69printEncryptionStatus(putResult);70}7172private static void changeSSEEncryptionStatusByCopying(AmazonS3 s3Client,73String bucketName,74String sourceKey,75String destKey) {76// Upload a new, unencrypted object.77PutObjectResult putResult = s3Client.putObject(bucketName, sourceKey, "Object example to encrypt by copying");78System.out.println("Unencrypted object \"" + sourceKey + "\" uploaded.");79printEncryptionStatus(putResult);8081// Make a copy of the object and use server-side encryption when storing the copy.82CopyObjectRequest request = new CopyObjectRequest(bucketName,83sourceKey,84bucketName,85destKey);86ObjectMetadata objectMetadata = new ObjectMetadata();87objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);88request.setNewObjectMetadata(objectMetadata);8990// Perform the copy operation and display the copy's encryption status.91CopyObjectResult response = s3Client.copyObject(request);92System.out.println("Object \"" + destKey + "\" uploaded with SSE.");93printEncryptionStatus(response);9495// Delete the original, unencrypted object, leaving only the encrypted copy in Amazon S3.96s3Client.deleteObject(bucketName, sourceKey);97System.out.println("Unencrypted object \"" + sourceKey + "\" deleted.");98}99100private static void printEncryptionStatus(SSEResultBase response) {101String encryptionStatus = response.getSSEAlgorithm();102if(encryptionStatus == null) {103encryptionStatus = "Not encrypted with SSE";104}105System.out.println("Object encryption status is: " + encryptionStatus);106}107}108109110