Python boto3 checking for valid bucket within region

Viewed 277

I have seen examples for checking whether an S3 bucket exists and have implemented them below. My bucket is located in us-east-1 region but the following code doesn't throw an exception. Is there a way to make the check region specific depending on my session?

session = boto3.Session(
    profile_name = 'TEST'
    ,region_name='ap-south-1'
)

s3 = session.resource('s3')

bucket_name = 'TEST_BUCKET'

try:
    s3.meta.client.head_bucket(Bucket = bucket_name)
except ClientError as c:
    print(c)
1 Answers

It does not matter which S3 regional endpoint you send the request to. The underlying SDK (boto3) will redirect as needed. It's preferable, however, to target the correct region if you know it in advance, to save on redirects.

You can see this in detail if you use the awscli in debug mode:

aws s3api head-bucket --bucket mybucket --region ap-south-1 --debug

You will see debug output similar to this:

DEBUG - S3 client configured for region ap-south-1 but the bucket mybucket is in region us-east-1; Please configure the proper region to avoid multiple unnecessary redirects and signing attempts.
DEBUG - Switching signature version for service s3 to version s3v4 based on config file override.
DEBUG - Updating URI from https://s3.ap-south-1.amazonaws.com/mybucket to https://s3.us-east-1.amazonaws.com/mybucket

Note that the awscli uses the boto3 SDK, as does your Python script.

Related