I am using boto3 for testing S3 api. My favorite cases are multi-regional.
For example, in code above I want to get redirect (301) from AWS, because I'am trying to delete bucket with another Location Constraint from main region.
# step 0: create client for main region
cli = session.client("s3", region_name="us-east-1")
# step 1: create bucket in us-west-2 (not main) region
cli.create_bucket(
Bucket=somename,
CreateBucketConfiguration={"LocationConstraint": "us-west-2"}
)
# step 2: try to delete this bucket
cli.delete_bucket(Bucket=somename)
And... i got 204.. but, i expected 301. Why?
You can't delete a bucket that is in a different location of client.
In botocore logs i found next lines:
2022-09-30 15:51:09,844 botocore.hooks [DEBUG] Event needs-retry.s3.DeleteBucket: calling handler <bound method S3RegionRedirector.redirect_from_error of <botocore.utils.S3RegionRedirector object at 0x108aab550>>
2022-09-30 15:51:09,844 botocore.utils [DEBUG] S3 client configured for region us-east-1 but the bucket testbucket2ffd929fin is in region us-west-2; Please configure the proper region to avoid multiple unnecessary redirects and signing attempts.
2022-09-30 15:51:09,844 botocore.utils [DEBUG] Updating URI from https://s3.amazonaws.com/testbucket2ffd929fin to https://s3.us-west-2.amazonaws.com/testbucket2ffd929fin
boto3 automatically redirects me to the correct url, but what I can do if I want to get a raw 301? It needed for testing.
After reading botocore sources, I understand how to do it.
You can use following code to disable auto redirect:
cli = session.client("s3")
deq = cli.meta.events._emitter._handlers.prefix_search("needs-retry.s3")
while len(deq) > 0:
cli.meta.events.unregister("needs-retry.s3", handler=deq.pop())