Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the documentation for creating s3 bucket to address the region mismatch issue. #4337

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions docs/source/guide/s3-example-creating-buckets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +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.

.. 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.
jonathan343 marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python
ubaskota marked this conversation as resolved.
Show resolved Hide resolved

import logging
import boto3
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
Expand All @@ -47,20 +55,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)
ubaskota marked this conversation as resolved.
Show resolved Hide resolved
location = {'LocationConstraint': region} if region != 'us-east-1' else {}
jonathan343 marked this conversation as resolved.
Show resolved Hide resolved
s3_client.create_bucket(
Bucket=bucket_name, CreateBucketConfiguration=location
)
except ClientError as e:
logging.error(e)
return False
return True


List existing buckets
=====================

Expand All @@ -76,4 +80,3 @@ List all the existing buckets for the AWS account.
print('Existing buckets:')
for bucket in response['Buckets']:
print(f' {bucket["Name"]}')

Loading