skip to Main Content

Documentation

import logging
import boto3
from botocore.exceptions import ClientError


def create_bucket(bucket_name, region=None):
    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)
    except ClientError as e:
        logging.error(e)
        return False
    return True

Testing:

create_bucket('test', 'us-west-2') Works as expected -> Please select a different name and try again

create_bucket('test') The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.

create_bucket('test', 'us-east-1') The us-east-1 location constraint is incompatible for the region specific endpoint this request was sent to.

What did I miss?

2

Answers


  1. Chosen as BEST ANSWER

    The problem is in the error message.

    Obviously, the bucket with the name test is already taken, but for some reason, instead of saying Please select a different name and try again, we see a message that misleads us.

    So the solution is simple - use a unique name for the bucket.

    I hope AWS will fix the error message ASAP but it's not a blocker for us anyway


  2. This creates a bucket in us-east-1:

    s3_client = boto3.client('s3', region_name='us-east-1')
    s3_client.create_bucket(Bucket='unique-name-here')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search