From 113bfcb4eacaa3346e28f1e5103928aaa4c47c83 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Sat, 3 Jun 2017 19:29:59 -0400 Subject: [PATCH] Fix duplicate bucket creation with LocationConstraint. Closes #970. --- moto/s3/responses.py | 6 ++++++ tests/test_s3/test_s3.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 115fe98ae..3b349d864 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -336,6 +336,12 @@ class ResponseObject(_TemplateEnvironmentMixin): self.backend.set_bucket_website_configuration(bucket_name, body) return "" else: + if body: + try: + region_name = xmltodict.parse(body)['CreateBucketConfiguration']['LocationConstraint'] + except KeyError: + pass + try: new_bucket = self.backend.create_bucket( bucket_name, region_name) diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 5c830a905..8841f9f71 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1207,6 +1207,22 @@ def test_boto3_bucket_create(): "utf-8").should.equal("some text") +@mock_s3 +def test_bucket_create_duplicate(): + s3 = boto3.resource('s3', region_name='us-west-2') + s3.create_bucket(Bucket="blah", CreateBucketConfiguration={ + 'LocationConstraint': 'us-west-2', + }) + with assert_raises(ClientError) as exc: + s3.create_bucket( + Bucket="blah", + CreateBucketConfiguration={ + 'LocationConstraint': 'us-west-2', + } + ) + exc.exception.response['Error']['Code'].should.equal('BucketAlreadyExists') + + @mock_s3 def test_boto3_bucket_create_eu_central(): s3 = boto3.resource('s3', region_name='eu-central-1')