diff --git a/botocore/utils.py b/botocore/utils.py index 314d30516d..a54f4a39d3 100644 --- a/botocore/utils.py +++ b/botocore/utils.py @@ -1776,6 +1776,10 @@ def redirect_from_error(self, request_dict, response, operation, **kwargs): 0 ].status_code in (301, 302, 307) is_permanent_redirect = error_code == 'PermanentRedirect' + is_opt_in_region_redirect = ( + error_code == 'IllegalLocationConstraintException' + and operation.name != 'CreateBucket' + ) if not any( [ is_special_head_object, @@ -1783,6 +1787,7 @@ def redirect_from_error(self, request_dict, response, operation, **kwargs): is_permanent_redirect, is_special_head_bucket, is_redirect_status, + is_opt_in_region_redirect, ] ): return diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 8a92bd6b06..6143183418 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1879,6 +1879,66 @@ def test_does_not_redirect_if_None_response(self): ) self.assertIsNone(redirect_response) + def test_redirects_on_illegal_location_constraint_from_opt_in_region(self): + request_dict = { + 'url': 'https://il-central-1.amazonaws.com/foo', + 'context': { + 's3_redirect': { + 'bucket': 'foo', + 'redirected': False, + 'params': {'Bucket': 'foo'}, + }, + 'signing': {}, + }, + } + response = ( + None, + { + 'Error': {'Code': 'IllegalLocationConstraintException'}, + 'ResponseMetadata': { + 'HTTPHeaders': {'x-amz-bucket-region': 'eu-central-1'} + }, + }, + ) + + self.operation.name = 'GetObject' + redirect_response = self.redirector.redirect_from_error( + request_dict, response, self.operation + ) + self.assertEqual(redirect_response, 0) + + def test_no_redirect_on_illegal_location_constraint_from_bad_location_constraint( + self, + ): + request_dict = { + 'url': 'https://us-west-2.amazonaws.com/foo', + 'context': { + 's3_redirect': { + 'bucket': 'foo', + 'redirected': False, + 'params': { + 'Bucket': 'foo', + 'CreateBucketConfiguration': { + 'LocationConstraint': 'eu-west-2', + }, + }, + }, + 'signing': {}, + }, + } + response = ( + None, + { + 'Error': {'Code': 'IllegalLocationConstraintException'}, + }, + ) + + self.operation.name = 'CreateBucket' + redirect_response = self.redirector.redirect_from_error( + request_dict, response, self.operation + ) + self.assertIsNone(redirect_response) + def test_get_region_from_response(self): response = ( None,