putBucketEncryption method
This operation configures default encryption and Amazon S3 Bucket Keys for an existing bucket. You can also block encryption types using this operation. By default, all buckets have a default encryption configuration that uses server-side encryption with Amazon S3 managed keys (SSE-S3).
-
General purpose buckets
- You can optionally configure default encryption for a bucket by using server-side encryption with Key Management Service (KMS) keys (SSE-KMS) or dual-layer server-side encryption with Amazon Web Services KMS keys (DSSE-KMS). If you specify default encryption by using SSE-KMS, you can also configure Amazon S3 Bucket Keys. For information about the bucket default encryption feature, see Amazon S3 Bucket Default Encryption in the Amazon S3 User Guide.
- If you use PutBucketEncryption to set your default bucket encryption to SSE-KMS, you should verify that your KMS key ID is correct. Amazon S3 doesn't validate the KMS key ID provided in PutBucketEncryption requests.
-
Directory buckets - You can optionally configure default
encryption for a bucket by using server-side encryption with Key
Management Service (KMS) keys (SSE-KMS).
-
We recommend that the bucket's default encryption uses the desired
encryption configuration and you don't override the bucket default
encryption in your
CreateSessionrequests orPUTobject requests. Then, new objects are automatically encrypted with the desired encryption settings. For more information about the encryption overriding behaviors in directory buckets, see Specifying server-side encryption with KMS for new object uploads. -
Your SSE-KMS configuration can only support 1 customer
managed key per directory bucket's lifetime. The Amazon
Web Services managed key (
aws/s3) isn't supported. -
S3 Bucket Keys are always enabled for
GETandPUToperations in a directory bucket and can’t be disabled. S3 Bucket Keys aren't supported, when you copy SSE-KMS encrypted objects from general purpose buckets to directory buckets, from directory buckets to general purpose buckets, or between directory buckets, through CopyObject, UploadPartCopy, the Copy operation in Batch Operations, or the import jobs. In this case, Amazon S3 makes a call to KMS every time a copy request is made for a KMS-encrypted object. - When you specify an KMS customer managed key for encryption in your directory bucket, only use the key ID or key ARN. The key alias format of the KMS key isn't supported.
- For directory buckets, if you use PutBucketEncryption to set your default bucket encryption to SSE-KMS, Amazon S3 validates the KMS key ID provided in PutBucketEncryption requests.
-
We recommend that the bucket's default encryption uses the desired
encryption configuration and you don't override the bucket default
encryption in your
Implementation
Future<void> putBucketEncryption({
required String bucket,
required ServerSideEncryptionConfiguration
serverSideEncryptionConfiguration,
ChecksumAlgorithm? checksumAlgorithm,
String? contentMD5,
String? expectedBucketOwner,
}) async {
final headers = <String, String>{
if (checksumAlgorithm != null)
'x-amz-sdk-checksum-algorithm': checksumAlgorithm.value,
if (contentMD5 != null) 'Content-MD5': contentMD5.toString(),
if (expectedBucketOwner != null)
'x-amz-expected-bucket-owner': expectedBucketOwner.toString(),
};
await _protocol.send(
method: 'PUT',
requestUri: '/${Uri.encodeComponent(bucket)}?encryption',
headers: headers,
payload: serverSideEncryptionConfiguration
.toXml('ServerSideEncryptionConfiguration'),
exceptionFnMap: _exceptionFns,
);
}