Skip to content

Commit 3fe1b11

Browse files
authored
Track S3 region redirect feature id (#3804)
S3RegionRedirectorv2 now registers the S3_REGION_REDIRECT feature id ("Ah") when it retries after a redirect error, and again when it reuses a cached bucket region on later requests.
1 parent b770c0d commit 3fe1b11

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎botocore/useragent.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
'FLEXIBLE_CHECKSUMS_REQ_XXHASH3': 'AG',
106106
'FLEXIBLE_CHECKSUMS_REQ_XXHASH64': 'AH',
107107
'FLEXIBLE_CHECKSUMS_REQ_XXHASH128': 'AI',
108+
'S3_REGION_REDIRECT': 'Ah',
108109
}
109110

110111

‎botocore/utils.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
reset_plugin_context,
9595
set_plugin_context,
9696
)
97+
from botocore.useragent import register_feature_id
9798

9899
logger = logging.getLogger(__name__)
99100
DEFAULT_METADATA_SERVICE_TIMEOUT = 1
@@ -1842,6 +1843,8 @@ def redirect_from_error(self, request_dict, response, operation, **kwargs):
18421843
bucket,
18431844
new_region,
18441845
)
1846+
register_feature_id('S3_REGION_REDIRECT')
1847+
18451848
# Adding the new region to _cache will make construct_endpoint() to
18461849
# use the new region as value for the AWS::Region builtin parameter.
18471850
self._cache[bucket] = new_region
@@ -1921,6 +1924,7 @@ def redirect_from_cache(self, builtins, params, **kwargs):
19211924
if bucket is not None and bucket in self._cache:
19221925
new_region = self._cache.get(bucket)
19231926
builtins['AWS::Region'] = new_region
1927+
register_feature_id('S3_REGION_REDIRECT')
19241928

19251929
def annotate_request_context(self, params, context, **kwargs):
19261930
"""Store the bucket name in context for later use when redirecting.

‎tests/functional/test_useragent.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,55 @@ def test_user_agent_has_registered_feature_id(patched_session):
220220
assert 'C' in feature_list
221221

222222

223+
def test_user_agent_has_s3_region_redirect_feature_id(patched_session):
224+
client_s3 = patched_session.create_client('s3', region_name='us-west-2')
225+
with ClientHTTPStubber(client_s3) as stub_client:
226+
# The first attempt is rejected because the bucket lives in another
227+
# region. The redirector registers `'S3_REGION_REDIRECT': 'Ah'` and
228+
# retries the request against the region from the error response.
229+
stub_client.add_response(
230+
status=301,
231+
headers={'x-amz-bucket-region': 'eu-central-1'},
232+
body=(
233+
b'<Error><Code>PermanentRedirect</Code>'
234+
b'<Bucket>mybucket</Bucket></Error>'
235+
),
236+
)
237+
stub_client.add_response()
238+
client_s3.list_objects_v2(Bucket='mybucket')
239+
240+
ua_strings = get_captured_ua_strings(stub_client)
241+
assert 'Ah' not in parse_registered_feature_ids(ua_strings[0])
242+
assert 'Ah' in parse_registered_feature_ids(ua_strings[1])
243+
244+
245+
def test_user_agent_has_s3_region_redirect_feature_id_from_cache(
246+
patched_session,
247+
):
248+
client_s3 = patched_session.create_client('s3', region_name='us-west-2')
249+
with ClientHTTPStubber(client_s3) as stub_client:
250+
# The first call redirects and caches the bucket's real region.
251+
stub_client.add_response(
252+
status=301,
253+
headers={'x-amz-bucket-region': 'eu-central-1'},
254+
body=(
255+
b'<Error><Code>PermanentRedirect</Code>'
256+
b'<Bucket>mybucket</Bucket></Error>'
257+
),
258+
)
259+
stub_client.add_response()
260+
client_s3.list_objects_v2(Bucket='mybucket')
261+
262+
# The second call is served from the redirect cache, so it never
263+
# sees an error response but is still a region redirect.
264+
stub_client.add_response()
265+
client_s3.list_objects_v2(Bucket='mybucket')
266+
267+
ua_strings = get_captured_ua_strings(stub_client)
268+
assert len(ua_strings) == 3
269+
assert 'Ah' in parse_registered_feature_ids(ua_strings[2])
270+
271+
223272
def test_registered_feature_ids_dont_bleed_between_requests(patched_session):
224273
client_s3 = patched_session.create_client('s3')
225274
with ClientHTTPStubber(client_s3) as stub_client:

0 commit comments

Comments
 (0)