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

S3 - Allow create_bucket(region=None), instead of throwing IllegalLocationConstraintException #3292

Closed
changliuit opened this issue Sep 8, 2020 · 14 comments

Comments

@changliuit
Copy link

changliuit commented Sep 8, 2020

Version: moto: 1.3.15; botocore: 1.17.56
python 3.7.4

was working before with moto 1.3.14, but stopped working in 1.3.15

error:

            error_code = parsed_response.get("Error", {}).get("Code")
            error_class = self.exceptions.from_code(error_code)
>           raise error_class(parsed_response, operation_name)
E           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.

../../../.venv/lib/python3.7/site-packages/botocore/client.py:656: ClientError

test as below:

    @pytest.fixture(scope='class')
    def s3_client(self):
        s3_client = boto3.client('s3', region_name='ap-southeast-2')
        yield s3_client

    @mock_s3
    def test_store_new_supplier_file(self, config, s3_client):
        s3_client.create_bucket(Bucket='test')
@bblommers
Copy link
Collaborator

Hi @changliuit, this mimics the behaviour from AWS itself.
If the region is anything else then the default (us-east-1), the Location has to be specified using the CreateBucketConfigurationOption. See this example: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-creating-buckets.html

@bblommers bblommers added the debugging Working with user to figure out if there is an issue label Sep 8, 2020
@changliuit
Copy link
Author

Hey @bblommers thanks for your reply! Right now the create_bucket is not exactly the same behaviour as the boto one which allows region=None in create_bucket function. Can we have something here similar to the boto code -- if region is None, get it from the client?

Regards
Chang

@bblommers
Copy link
Collaborator

Hi @changliuit, I wasn't aware of the region=None behaviour! Will mark it as an enhancement

@bblommers bblommers added enhancement and removed debugging Working with user to figure out if there is an issue labels Sep 8, 2020
@bblommers bblommers changed the title IllegalLocationConstraintException when creating bucket S3 - Allow create_bucket(region=None), instead of throwing IllegalLocationConstraintException Sep 8, 2020
@changliuit
Copy link
Author

thanks @bblommers that'll be great

codeboten pushed a commit to codeboten/opentelemetry-python that referenced this issue Sep 11, 2020
The failing test was introduced I believe by a change in behaviour noted in this issue: getmoto/moto#3292. The solution is to set a region in the create_bucket call.

Fixes open-telemetry#1088
codeboten pushed a commit to open-telemetry/opentelemetry-python that referenced this issue Sep 14, 2020
The failing test was introduced I believe by a change in behaviour noted in this issue: getmoto/moto#3292. The solution is to set a region in the create_bucket call.

Fixes #1088
@jmehnle
Copy link
Contributor

jmehnle commented Sep 17, 2020

I understand I can work around this by specifying a region, but I choose not to (I have too many create_bucket calls in my unit tests, and running on moto 1.3.14 for a while longer is the lesser evil), and so this is effectively preventing me from upgrading to moto 1.3.16. :(

@bblommers
Copy link
Collaborator

Hi @changliuit, can you share an example test of the region=none?
I assumed you meant s3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={"LocationConstraint": None},), but that doesn't work anyway.

NathanielRN pushed a commit to NathanielRN/opentelemetry-python-contrib-1 that referenced this issue Oct 20, 2020
The failing test was introduced I believe by a change in behaviour noted in this issue: getmoto/moto#3292. The solution is to set a region in the create_bucket call.

Fixes open-telemetry#1088
@christian-ertl
Copy link

Moto 1.3.16 also fails for s3.create_bucket(Bucket=bucket_name).

codeboten pushed a commit to open-telemetry/opentelemetry-python-contrib that referenced this issue Oct 22, 2020
The failing test was introduced I believe by a change in behaviour noted in this issue: getmoto/moto#3292. The solution is to set a region in the create_bucket call.

Fixes #1088
@wmarcuse
Copy link

I got this error with mocking S3, seems to be the trick that the mocked S3 client only accepts region us-east-1. As it is a mocked bucket it doesn't matter..

    conn = boto3.resource("s3", region_name="us-east-1")
    conn.create_bucket(Bucket=os.environ["S3_BUCKET_NAME"]

@kaito-albert
Copy link

kaito-albert commented Nov 12, 2020

I got this error even with setting to region=ap-northeast-1.
Don't forget tokyo!

alexandru-m-g added a commit to OCHA-DAP/hdx-ckan that referenced this issue Jan 7, 2021
@PuneethaPai
Copy link

I was getting the same error, because mock s3 expects default region to be us-east-1.
We can use env variable to do so, instead of making code changes in all test cases:

export AWS_DEFAULT_REGION=us-east-1

@andreif
Copy link

andreif commented Jan 7, 2022

Ended up with

os.environ["AWS_DEFAULT_REGION"] = moto.s3.responses.DEFAULT_REGION_NAME = "eu-west-1"

@bblommers
Copy link
Collaborator

Closing this, as the original report was not a bug, as far as I'm concerned.

@forzagreen
Copy link
Contributor

Works with moto==3.1.4:

s3_client.create_bucket(
    Bucket="test",
    CreateBucketConfiguration={"LocationConstraint": "eu-west-1"}
)

GeoWill added a commit to DemocracyClub/UK-Polling-Stations that referenced this issue Mar 2, 2023
GeoWill added a commit to DemocracyClub/UK-Polling-Stations that referenced this issue Mar 6, 2023
@d33tah
Copy link

d33tah commented Sep 27, 2024

Is eu-west-1 somehow special here? I'm still getting this error after trying the advice here - with the exception of not being able to set moto.s3.responses.DEFAULT_REGION_NAME because I'm using it through docker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants