From 513e00d6777fed77c54d27cd2d0ff636e88e4e5f Mon Sep 17 00:00:00 2001 From: Ujjwal Baskota Date: Thu, 7 Nov 2024 19:50:12 -0500 Subject: [PATCH 1/5] update the documentation for creating s3 bucket to address the region mismatch issue. --- .../guide/s3-example-creating-buckets.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/source/guide/s3-example-creating-buckets.rst b/docs/source/guide/s3-example-creating-buckets.rst index 7471a59504..91f59f002d 100644 --- a/docs/source/guide/s3-example-creating-buckets.rst +++ b/docs/source/guide/s3-example-creating-buckets.rst @@ -27,6 +27,22 @@ 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. +.. warning:: + + You need to make sure that the region in your aws configuration matches the + region where you want your bucket to be created. By default, s3 will create + a bucket in ``us-east-1`` if the ``LocationConstraint`` is not specified. + However, if the region in your configuration doesn't match the + ``LocationConstraint``, you'll get ``IllegalLocationConstraintException`` + error when calling ``create_bucket`` function. + + To avoid this error, you should either: + + * Option 1: Make sure that your ``LocationConstraint`` is same as the region in your + configuration, or + * Option 2: If you're using a default value for ``LocationConstraint``, make sure + that the region in your configuration is set to ``us-east-1``. + .. code-block:: python import logging @@ -61,6 +77,8 @@ or to address regulatory requirements. return True + + List existing buckets ===================== From 5b3637ba2cf9f433cd55e735c3d92f947369fe84 Mon Sep 17 00:00:00 2001 From: Ujjwal Baskota Date: Tue, 12 Nov 2024 10:36:55 -0500 Subject: [PATCH 2/5] Removes the warning message and modifies the example code --- .../guide/s3-example-creating-buckets.rst | 35 ++++--------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/docs/source/guide/s3-example-creating-buckets.rst b/docs/source/guide/s3-example-creating-buckets.rst index 91f59f002d..74c32a4c14 100644 --- a/docs/source/guide/s3-example-creating-buckets.rst +++ b/docs/source/guide/s3-example-creating-buckets.rst @@ -27,22 +27,6 @@ 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. -.. warning:: - - You need to make sure that the region in your aws configuration matches the - region where you want your bucket to be created. By default, s3 will create - a bucket in ``us-east-1`` if the ``LocationConstraint`` is not specified. - However, if the region in your configuration doesn't match the - ``LocationConstraint``, you'll get ``IllegalLocationConstraintException`` - error when calling ``create_bucket`` function. - - To avoid this error, you should either: - - * Option 1: Make sure that your ``LocationConstraint`` is same as the region in your - configuration, or - * Option 2: If you're using a default value for ``LocationConstraint``, make sure - that the region in your configuration is set to ``us-east-1``. - .. code-block:: python import logging @@ -50,7 +34,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 @@ -63,22 +47,16 @@ 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) + s3_client = boto3.client('s3', region_name=region) + location = {'LocationConstraint': region} if region != 'us-east-1' else {} + s3_client.create_bucket( + Bucket=bucket_name, CreateBucketConfiguration=location + ) except ClientError as e: logging.error(e) return False return True - - - List existing buckets ===================== @@ -94,4 +72,3 @@ List all the existing buckets for the AWS account. print('Existing buckets:') for bucket in response['Buckets']: print(f' {bucket["Name"]}') - From 12e24d8258c8e68414003b8bb0e211c8d4575d38 Mon Sep 17 00:00:00 2001 From: Ujjwal Baskota <19787410+ubaskota@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:44:56 -0500 Subject: [PATCH 3/5] adds a note to describe the role of region in bucket creation --- docs/source/guide/s3-example-creating-buckets.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/source/guide/s3-example-creating-buckets.rst b/docs/source/guide/s3-example-creating-buckets.rst index 74c32a4c14..ed89823949 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 From 72557dc7f11ea4df54ceab8fc5d4a273d77aaa47 Mon Sep 17 00:00:00 2001 From: Ujjwal Baskota <19787410+ubaskota@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:28:56 -0500 Subject: [PATCH 4/5] fixes the issue in the example code and rendering the test --- .gitignore | 2 ++ .../guide/s3-example-creating-buckets.rst | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index b4e0995060..7ea6ff39d8 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,8 @@ coverage.xml venv env2 env3 +boto3_venv/ + # Virtualenv support files and directories .python-version diff --git a/docs/source/guide/s3-example-creating-buckets.rst b/docs/source/guide/s3-example-creating-buckets.rst index ed89823949..e137d5baec 100644 --- a/docs/source/guide/s3-example-creating-buckets.rst +++ b/docs/source/guide/s3-example-creating-buckets.rst @@ -28,12 +28,12 @@ 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 + 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. + ``IllegalLocationConstraintException`` error. .. code-block:: python @@ -55,11 +55,12 @@ or to address regulatory requirements. # Create bucket try: + bucket_config = {} s3_client = boto3.client('s3', region_name=region) - location = {'LocationConstraint': region} if region != 'us-east-1' else {} - s3_client.create_bucket( - Bucket=bucket_name, CreateBucketConfiguration=location - ) + 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 From 48cb4b91ab28a222005b85b2eb3085da4230d0f6 Mon Sep 17 00:00:00 2001 From: Ujjwal Baskota <19787410+ubaskota@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:01:17 -0500 Subject: [PATCH 5/5] fixes some formatting changes --- .gitignore | 2 -- docs/source/guide/s3-example-creating-buckets.rst | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7ea6ff39d8..b4e0995060 100644 --- a/.gitignore +++ b/.gitignore @@ -18,8 +18,6 @@ coverage.xml venv env2 env3 -boto3_venv/ - # Virtualenv support files and directories .python-version diff --git a/docs/source/guide/s3-example-creating-buckets.rst b/docs/source/guide/s3-example-creating-buckets.rst index e137d5baec..57a9ba8e8c 100644 --- a/docs/source/guide/s3-example-creating-buckets.rst +++ b/docs/source/guide/s3-example-creating-buckets.rst @@ -57,8 +57,8 @@ or to address regulatory requirements. try: bucket_config = {} s3_client = boto3.client('s3', region_name=region) - if region != "us-east-1": - bucket_config["CreateBucketConfiguration"] = {"LocationConstraint": region} + if region != 'us-east-1': + bucket_config['CreateBucketConfiguration'] = {'LocationConstraint': region} s3_client.create_bucket(Bucket=bucket_name, **bucket_config) except ClientError as e: