putBucketPolicy method

Future<void> putBucketPolicy({
  1. required String bucket,
  2. required String policy,
  3. bool? confirmRemoveSelfBucketAccess,
  4. String? contentMD5,
  5. String? expectedBucketOwner,
})

Applies an Amazon S3 bucket policy to an Amazon S3 bucket. If you are using an identity other than the root user of the AWS account that owns the bucket, the calling identity must have the PutBucketPolicy permissions on the specified bucket and belong to the bucket owner's account in order to use this operation.

If you don't have PutBucketPolicy permissions, Amazon S3 returns a 403 Access Denied error. If you have the correct permissions, but you're not using an identity that belongs to the bucket owner's account, Amazon S3 returns a 405 Method Not Allowed error. For more information about bucket policies, see Using Bucket Policies and User Policies.

The following operations are related to PutBucketPolicy:

Parameter bucket : The name of the bucket.

Parameter policy : The bucket policy as a JSON document.

Parameter confirmRemoveSelfBucketAccess : Set this parameter to true to confirm that you want to remove your permissions to change this bucket policy in the future.

Parameter contentMD5 : The MD5 hash of the request body.

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> putBucketPolicy({
  required String bucket,
  required String policy,
  bool? confirmRemoveSelfBucketAccess,
  String? contentMD5,
  String? expectedBucketOwner,
}) async {
  ArgumentError.checkNotNull(bucket, 'bucket');
  ArgumentError.checkNotNull(policy, 'policy');
  final headers = <String, String>{
    if (confirmRemoveSelfBucketAccess != null)
      'x-amz-confirm-remove-self-bucket-access':
          confirmRemoveSelfBucketAccess.toString(),
    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)}?policy',
    headers: headers,
    payload: policy,
    exceptionFnMap: _exceptionFns,
  );
}