createBucket method

Future<CreateBucketOutput> createBucket({
  1. required String bucket,
  2. BucketCannedACL? acl,
  3. BucketNamespace? bucketNamespace,
  4. CreateBucketConfiguration? createBucketConfiguration,
  5. String? grantFullControl,
  6. String? grantRead,
  7. String? grantReadACP,
  8. String? grantWrite,
  9. String? grantWriteACP,
  10. bool? objectLockEnabledForBucket,
  11. ObjectOwnership? objectOwnership,
})
Creates a new S3 bucket. To create a bucket, you must set up Amazon S3 and have a valid Amazon Web Services Access Key ID to authenticate requests. Anonymous requests are never allowed to create buckets. By creating the bucket, you become the bucket owner.

There are two types of buckets: general purpose buckets and directory buckets. For more information about these bucket types, see Creating, configuring, and working with Amazon S3 buckets in the Amazon S3 User Guide.

General purpose buckets exist in a global namespace, which means that each bucket name must be unique across all Amazon Web Services accounts in all the Amazon Web Services Regions within a partition. A partition is a grouping of Regions. Amazon Web Services currently has four partitions: aws (Standard Regions), aws-cn (China Regions), aws-us-gov (Amazon Web Services GovCloud (US)), and aws-eusc (European Sovereign Cloud). When you create a general purpose bucket, you can choose to create a bucket in the shared global namespace or you can choose to create a bucket in your account regional namespace. Your account regional namespace is a subdivision of the global namespace that only your account can create buckets in. For more information on account regional namespaces, see Namespaces for general purpose buckets.

  • General purpose buckets - If you send your CreateBucket request to the s3.amazonaws.com global endpoint, the request goes to the us-east-1 Region. So the signature calculations in Signature Version 4 must use us-east-1 as the Region, even if the location constraint in the request specifies another Region where the bucket is to be created. If you create a bucket in a Region other than US East (N. Virginia), your application must be able to handle 307 redirect. For more information, see Virtual hosting of buckets in the Amazon S3 User Guide.
  • Directory buckets - For directory buckets, you must make requests for this API operation to the Regional endpoint. These endpoints support path-style requests in the format https://s3express-control.region-code.amazonaws.com/bucket-name . Virtual-hosted-style requests aren't supported. For more information about endpoints in Availability Zones, see Regional and Zonal endpoints for directory buckets in Availability Zones in the Amazon S3 User Guide. For more information about endpoints in Local Zones, see Concepts for directory buckets in Local Zones in the Amazon S3 User Guide.
Permissions
  • General purpose bucket permissions - In addition to the s3:CreateBucket permission, the following permissions are required in a policy when your CreateBucket request includes specific headers:
    • Access control lists (ACLs) - In your CreateBucket request, if you specify an access control list (ACL) and set it to public-read, public-read-write, authenticated-read, or if you explicitly specify any other custom ACLs, both s3:CreateBucket and s3:PutBucketAcl permissions are required. In your CreateBucket request, if you set the ACL to private, or if you don't specify any ACLs, only the s3:CreateBucket permission is required.
    • Object Lock - In your CreateBucket request, if you set x-amz-bucket-object-lock-enabled to true, the s3:PutBucketObjectLockConfiguration and s3:PutBucketVersioning permissions are required.
    • S3 Object Ownership - If your CreateBucket request includes the x-amz-object-ownership header, then the s3:PutBucketOwnershipControls permission is required.
    • S3 Block Public Access - If your specific use case requires granting public access to your S3 resources, you can disable Block Public Access. Specifically, you can create a new bucket with Block Public Access enabled, then separately call the DeletePublicAccessBlock API. To use this operation, you must have the s3:PutBucketPublicAccessBlock permission. For more information about S3 Block Public Access, see Blocking public access to your Amazon S3 storage in the Amazon S3 User Guide.
  • Directory bucket permissions - You must have the s3express:CreateBucket permission in an IAM identity-based policy instead of a bucket policy. Cross-account access to this API operation isn't supported. This operation can only be performed by the Amazon Web Services account that owns the resource. For more information about directory bucket policies and permissions, see Amazon Web Services Identity and Access Management (IAM) for S3 Express One Zone in the Amazon S3 User Guide.
HTTP Host header syntax
Directory buckets - The HTTP Host header syntax is s3express-control.region-code.amazonaws.com.
The following operations are related to CreateBucket:

May throw BucketAlreadyExists. May throw BucketAlreadyOwnedByYou.

Parameter bucket : The name of the bucket to create.

General purpose buckets - For information about bucket naming restrictions, see Bucket naming rules in the Amazon S3 User Guide.

Directory buckets - When you use this operation with a directory bucket, you must use path-style requests in the format https://s3express-control.region-code.amazonaws.com/bucket-name . Virtual-hosted-style requests aren't supported. Directory bucket names must be unique in the chosen Zone (Availability Zone or Local Zone). Bucket names must also follow the format bucket-base-name--zone-id--x-s3 (for example, DOC-EXAMPLE-BUCKET--usw2-az1--x-s3). For information about bucket naming restrictions, see Directory bucket naming rules in the Amazon S3 User Guide

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

Parameter bucketNamespace : Specifies the namespace where you want to create your general purpose bucket. When you create a general purpose bucket, you can choose to create a bucket in the shared global namespace or you can choose to create a bucket in your account regional namespace. Your account regional namespace is a subdivision of the global namespace that only your account can create buckets in. For more information on bucket namespaces, see Namespaces for general purpose buckets.

General purpose buckets in your account regional namespace must follow a specific naming convention. These buckets consist of a bucket name prefix that you create, and a suffix that contains your 12-digit Amazon Web Services Account ID, the Amazon Web Services Region code, and ends with -an. Bucket names must follow the format bucket-name-prefix-accountId-region-an (for example, amzn-s3-demo-bucket-111122223333-us-west-2-an). For information about bucket naming restrictions, see Account regional namespace naming rules in the Amazon S3 User Guide.

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 new objects in the bucket.

For the bucket and object owners of existing objects, also allows deletions and overwrites of those objects.

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,
  BucketNamespace? bucketNamespace,
  CreateBucketConfiguration? createBucketConfiguration,
  String? grantFullControl,
  String? grantRead,
  String? grantReadACP,
  String? grantWrite,
  String? grantWriteACP,
  bool? objectLockEnabledForBucket,
  ObjectOwnership? objectOwnership,
}) async {
  final headers = <String, String>{
    if (acl != null) 'x-amz-acl': acl.value,
    if (bucketNamespace != null)
      'x-amz-bucket-namespace': bucketNamespace.value,
    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(),
    if (objectOwnership != null)
      'x-amz-object-ownership': objectOwnership.value,
  };
  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(
    bucketArn:
        _s.extractHeaderStringValue($result.headers, 'x-amz-bucket-arn'),
    location: _s.extractHeaderStringValue($result.headers, 'Location'),
  );
}