I’m trying to create a bucket using:
s3_client = boto3.client('s3',
aws_access_key_id=api_id,
aws_secret_access_key=apikey,
region_name='us-east-1')
bucket_name = 'test1'
s3_client.create_bucket(Bucket=bucket_name)
I’ve seen this same code everywhere all over SO and Github and it’s meant to work but I’m getting this exception:
botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.
Which from my research means I should specify the region (I did so in the client here), I also tried this line (using us-east-2
just to test a different region):
s3_client.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={'LocationConstraint': 'us-east-2'})
But that still throws an exception:
botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The us-east-2 location constraint is incompatible for the region specific endpoint this request was sent to.
I also tried this code:
s3_client = boto3.client('s3',
aws_access_key_id=api_id,
aws_secret_access_key=apikey)
bucket_name = 'test1'
s3_client.create_bucket(Bucket=bucket_name)
And my .aws config file specified:
[default]
region = us-east-1
output = json
As far as I can tell, one of these methods should have worked. If it were an auth issue, I’d have gotten that exception instead.
Can anyone point me in the right direction?
3
Answers
It works as long as I don't try to write into us-east-1, any of the other regions work just fine.
Refer to the AWS Code Library that has the latest SDK examples for Python. All code examples you find in this doc have been tested many times and work.
This is the document to refer to for finding AWS SDK code examples and contain the latest code examples.
See this topic:
Create an Amazon S3 bucket using an AWS SDK
This example includes many different SDK examples including Python.
When your code runs, it sends requests to an AWS endpoint in the desired Region.
Since your configuration file has
region = us-east-1
, the request is being sent to the North Virginia region.Amazon S3 in that region can only create buckets in ‘itself’ (
us-east-1
). Therefore, you either need to specify{'LocationConstraint': 'us-east-2'}
OR you need to connect to Amazon S3 in the region where you want to create the bucket:Simply put, the region you are connecting to must match the
LocationConstraint
when creating an Amazon S3 bucket.