putBucketEncryption method

Future<void> putBucketEncryption({
  1. required String bucket,
  2. required ServerSideEncryptionConfiguration serverSideEncryptionConfiguration,
  3. String? contentMD5,
  4. String? expectedBucketOwner,
})

This operation uses the encryption subresource to configure default encryption and Amazon S3 Bucket Key for an existing bucket.

Default encryption for a bucket can use server-side encryption with Amazon S3-managed keys (SSE-S3) or AWS KMS customer master keys (SSE-KMS). If you specify default encryption using SSE-KMS, you can also configure Amazon S3 Bucket Key. For information about default encryption, see Amazon S3 default bucket encryption in the Amazon Simple Storage Service Developer Guide. For more information about S3 Bucket Keys, see Amazon S3 Bucket Keys in the Amazon Simple Storage Service Developer Guide. To use this operation, you must have permissions to perform the s3:PutEncryptionConfiguration action. The bucket owner has this permission by default. The bucket owner can grant this permission to others. For more information about permissions, see Permissions Related to Bucket Subresource Operations and Managing Access Permissions to Your Amazon S3 Resources in the Amazon Simple Storage Service Developer Guide.

Related Resources

Parameter bucket : Specifies default encryption for a bucket using server-side encryption with Amazon S3-managed keys (SSE-S3) or customer master keys stored in AWS KMS (SSE-KMS). For information about the Amazon S3 default encryption feature, see Amazon S3 Default Bucket Encryption in the Amazon Simple Storage Service Developer Guide.

Parameter contentMD5 : The base64-encoded 128-bit MD5 digest of the server-side encryption configuration.

For requests made using the AWS Command Line Interface (CLI) or AWS SDKs, this field is calculated automatically.

Parameter expectedBucketOwner : The account id of the expected bucket owner. If the bucket is owned by a different account, the request will fail with an HTTP 403 (Access Denied) error.

Implementation

Future<void> putBucketEncryption({
  required String bucket,
  required ServerSideEncryptionConfiguration
      serverSideEncryptionConfiguration,
  String? contentMD5,
  String? expectedBucketOwner,
}) async {
  ArgumentError.checkNotNull(bucket, 'bucket');
  ArgumentError.checkNotNull(
      serverSideEncryptionConfiguration, 'serverSideEncryptionConfiguration');
  final headers = <String, String>{
    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,
  );
}