Server-Side Encryption with Customer-Provided Encryption Keys Using the AWS SDK for Java
Specifying Server-Side Encryption with Customer-Provided Encryption Keys Using the AWS SDK for Java
The following example shows how to request server-side encryption with customer-provided keys (SSE-C) for objects. The example performs the following operations. Each operation shows how to specify SSE-C-related headers in the request:
- Put object—Uploads an object and requests server-side encryption using a customer-provided encryption key.
- Get object—Downloads the object uploaded in the previous step. In the request, you provide the same encryption information that you provided when you uploaded the object. Amazon S3 needs this information to decrypt the object so that it can return it to you.
- Get object metadata—Retrieves the object's metadata. You provide the same encryption information used when the object was created.
- Copy object—Makes a copy of the previously uploaded object. Because the source object is stored using SSE-C, you must provide its encryption information in your copy request. By default, Amazon S3 encrypts the copy of the object only if you explicitly request it. This example directs Amazon S3 to store an encrypted copy of the object using a new SSE-C key.
Note
This example shows how to upload an object in a single operation. When using the Multipart Upload API to upload large objects, you provide encryption information in the same way shown in this example. For examples of multipart uploads that use the AWS SDK for Java, see Using the AWS Java SDK for Multipart Upload (High-Level API) and Using the AWS Java SDK for a Multipart Upload (Low-Level API).
To add the required encryption information, you include an SSECustomerKey
in your request. For more information about the SSECustomerKey
class, see Using SSE-C.
For information about SSE-C, see Protecting Data Using Server-Side Encryption with Customer-Provided Encryption Keys (SSE-C). For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.
Example
Other Amazon S3 Operations with SSE-C Using the AWS SDK for Java
The example in the preceding section shows how to request server-side encryption with customer-provided keys (SSE-C) in the PUT, GET, Head, and Copy operations. This section describes other APIs that support SSE-C.
To upload large objects, you can use multipart upload API (see Uploading Objects Using Multipart Upload API). You can use either high-level or low-level APIs to upload large objects. These APIs support encryption-related headers in the request.
- When using the high-level
TransferManager
API, you provide the encryption-specific headers in thePutObjectRequest
(see Using the AWS Java SDK for Multipart Upload (High-Level API)). - When using the low-level API, you provide encryption-related information in the
InitiateMultipartUploadRequest
, followed by identical encryption information in eachUploadPartRequest
. You do not need to provide any encryption-specific headers in yourCompleteMultipartUploadRequest
. For examples, see Using the AWS Java SDK for a Multipart Upload (Low-Level API).
The following example uses TransferManager
to create objects and shows how to provide SSE-C related information. The example does the following:
- Creates an object using the
TransferManager.upload()
method. In thePutObjectRequest
instance, you provide encryption key information to request. Amazon S3 encrypts the object using the customer-provided encryption key. - Makes a copy of the object by calling the
TransferManager.copy()
method. The example directs Amazon S3 to encrypt the object copy using a newSSECustomerKey
. Because the source object is encrypted using SSE-C, theCopyObjectRequest
also provides the encryption key of the source object so that Amazon S3 can decrypt the object before copying it.
Example