SSEUsingJavaSDK
Specifying Server-Side Encryption Using the AWS SDK for Java
When you use the AWS SDK for Java to upload an object, you can use server-side encryption to encrypt it. To request server-side encryption, use the ObjectMetadata
property of the PutObjectRequest
to set the x-amz-server-side-encryption
request header. When you call the putObject()
method of the AmazonS3Client
, Amazon S3 encrypts and saves the data.
You can also request server-side encryption when uploading objects with the multipart upload API:
- When using the high-level multipart upload API, you use the
TransferManager
methods to apply server-side encryption to objects as you upload them. You can use any of the upload methods that takeObjectMetadata
as a parameter. For more information, see Using the AWS Java SDK for Multipart Upload (High-Level API). - When using the low-level multipart upload API, you specify server-side encryption when you initiate the multipart upload. You add the
ObjectMetadata
property by calling theInitiateMultipartUploadRequest.setObjectMetadata()
method. For more information, see Upload a File.
You can't directly change the encryption state of an object (encrypting an unencrypted object or decrypting an encrypted object). To change an object's encryption state, you make a copy of the object, specifying the desired encryption state for the copy, and then delete the original object. Amazon S3 encrypts the copied object only if you explicitly request server-side encryption. To request encryption of the copied object through the Java API, use the ObjectMetadata
property to specify server-side encryption in the CopyObjectRequest
.
Example Example
The following example shows how to set server-side encryption using the AWS SDK for Java. It shows how to perform the following tasks:
- Upload a new object using server-side encryption.
- Change an object's encryption state (in this example, encrypting a previously unencrypted object) by making a copy of the object.
- Check the encryption state of the object. For more information about server-side encryption, see Specifying Server-Side Encryption Using the REST API. For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.