diff --git a/docs/source/guide/s3-example-creating-buckets.rst b/docs/source/guide/s3-example-creating-buckets.rst index 7471a59504..57a9ba8e8c 100644 --- a/docs/source/guide/s3-example-creating-buckets.rst +++ b/docs/source/guide/s3-example-creating-buckets.rst @@ -27,6 +27,14 @@ The name of an Amazon S3 bucket must be unique across all regions of the AWS platform. The bucket can be located in a specific region to minimize latency or to address regulatory requirements. +.. note:: + The ``LocationConstraint`` value is used to specify the region where a bucket + will be created. S3 requires ``LocationConstraint`` to be specified when creating + buckets using a client in regions other than ``us-east-1``. When no region is + specified, ``us-east-1`` is used by default. The example below ensures the S3 + client is created in the same region as the bucket to avoid a + ``IllegalLocationConstraintException`` error. + .. code-block:: python import logging @@ -34,7 +42,7 @@ or to address regulatory requirements. from botocore.exceptions import ClientError - def create_bucket(bucket_name, region=None): + def create_bucket(bucket_name, region='us-east-1'): """Create an S3 bucket in a specified region If a region is not specified, the bucket is created in the S3 default @@ -47,20 +55,17 @@ or to address regulatory requirements. # Create bucket try: - if region is None: - s3_client = boto3.client('s3') - s3_client.create_bucket(Bucket=bucket_name) - else: - s3_client = boto3.client('s3', region_name=region) - location = {'LocationConstraint': region} - s3_client.create_bucket(Bucket=bucket_name, - CreateBucketConfiguration=location) + bucket_config = {} + s3_client = boto3.client('s3', region_name=region) + if region != 'us-east-1': + bucket_config['CreateBucketConfiguration'] = {'LocationConstraint': region} + + s3_client.create_bucket(Bucket=bucket_name, **bucket_config) except ClientError as e: logging.error(e) return False return True - List existing buckets ===================== @@ -76,4 +81,3 @@ List all the existing buckets for the AWS account. print('Existing buckets:') for bucket in response['Buckets']: print(f' {bucket["Name"]}') -