createBucket method

Future<CreateBucketOutput> createBucket({
  1. required String bucket,
  2. BucketCannedACL? acl,
  3. CreateBucketConfiguration? createBucketConfiguration,
  4. String? grantFullControl,
  5. String? grantRead,
  6. String? grantReadACP,
  7. String? grantWrite,
  8. String? grantWriteACP,
  9. bool? objectLockEnabledForBucket,
})

Creates a new S3 bucket. To create a bucket, you must register with Amazon S3 and have a valid AWS Access Key ID to authenticate requests. Anonymous requests are never allowed to create buckets. By creating the bucket, you become the bucket owner.

Not every string is an acceptable bucket name. For information about bucket naming restrictions, see Working with Amazon S3 buckets.

If you want to create an Amazon S3 on Outposts bucket, see Create Bucket.

By default, the bucket is created in the US East (N. Virginia) Region. You can optionally specify a Region in the request body. You might choose a Region to optimize latency, minimize costs, or address regulatory requirements. For example, if you reside in Europe, you will probably find it advantageous to create buckets in the Europe (Ireland) Region. For more information, see Accessing a bucket. When creating a bucket using this operation, you can optionally specify the accounts or groups that should be granted specific permissions on the bucket. There are two ways to grant the appropriate permissions using the request headers.

  • Specify a canned ACL using the x-amz-acl request header. Amazon S3 supports a set of predefined ACLs, known as canned ACLs. Each canned ACL has a predefined set of grantees and permissions. For more information, see Canned ACL.
  • Specify access permissions explicitly using the x-amz-grant-read, x-amz-grant-write, x-amz-grant-read-acp, x-amz-grant-write-acp, and x-amz-grant-full-control headers. These headers map to the set of permissions Amazon S3 supports in an ACL. For more information, see Access control list (ACL) overview.

    You specify each grantee as a type=value pair, where the type is one of the following:

    • id – if the value specified is the canonical user ID of an AWS account
    • uri – if you are granting permissions to a predefined group
    • emailAddress – if the value specified is the email address of an AWS account
    For example, the following x-amz-grant-read header grants the AWS accounts identified by account IDs permissions to read object data and its metadata:

    x-amz-grant-read: id="11112222333", id="444455556666"

The following operations are related to CreateBucket:

May throw BucketAlreadyExists. May throw BucketAlreadyOwnedByYou.

Parameter bucket : The name of the bucket to create.

Parameter acl : The canned ACL to apply to the bucket.

Parameter createBucketConfiguration : The configuration information for the bucket.

Parameter grantFullControl : Allows grantee the read, write, read ACP, and write ACP permissions on the bucket.

Parameter grantRead : Allows grantee to list the objects in the bucket.

Parameter grantReadACP : Allows grantee to read the bucket ACL.

Parameter grantWrite : Allows grantee to create, overwrite, and delete any object in the bucket.

Parameter grantWriteACP : Allows grantee to write the ACL for the applicable bucket.

Parameter objectLockEnabledForBucket : Specifies whether you want S3 Object Lock to be enabled for the new bucket.

Implementation

Future<CreateBucketOutput> createBucket({
  required String bucket,
  BucketCannedACL? acl,
  CreateBucketConfiguration? createBucketConfiguration,
  String? grantFullControl,
  String? grantRead,
  String? grantReadACP,
  String? grantWrite,
  String? grantWriteACP,
  bool? objectLockEnabledForBucket,
}) async {
  ArgumentError.checkNotNull(bucket, 'bucket');
  final headers = <String, String>{
    if (acl != null) 'x-amz-acl': acl.toValue(),
    if (grantFullControl != null)
      'x-amz-grant-full-control': grantFullControl.toString(),
    if (grantRead != null) 'x-amz-grant-read': grantRead.toString(),
    if (grantReadACP != null) 'x-amz-grant-read-acp': grantReadACP.toString(),
    if (grantWrite != null) 'x-amz-grant-write': grantWrite.toString(),
    if (grantWriteACP != null)
      'x-amz-grant-write-acp': grantWriteACP.toString(),
    if (objectLockEnabledForBucket != null)
      'x-amz-bucket-object-lock-enabled':
          objectLockEnabledForBucket.toString(),
  };
  final $result = await _protocol.sendRaw(
    method: 'PUT',
    requestUri: '/${Uri.encodeComponent(bucket)}',
    headers: headers,
    payload: createBucketConfiguration?.toXml('CreateBucketConfiguration'),
    exceptionFnMap: _exceptionFns,
  );
  final $elem = await _s.xmlFromResponse($result);
  return CreateBucketOutput(
    location: _s.extractHeaderStringValue($result.headers, 'Location'),
  );
}