我正在使用 boto3 来测试 S3 api。我最喜欢的案例是多区域的。
例如,在上面的代码中,我想从 AWS 获取重定向 (301),因为我正在尝试从主区域删除具有另一个位置约束的存储桶。
# 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)
而且...我得到了 204..但是,我期望 301。为什么?
您无法删除位于客户端不同位置的存储桶。
在 botocore 日志中我找到了下一行:
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 会自动将我重定向到正确的 url,但如果我想获得原始 301 该怎么办?需要进行测试。
最佳答案
阅读 botocore 源代码后,我明白了如何做到这一点。
您可以使用以下代码禁用自动重定向:
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())
关于amazon-s3 - 我可以在 boto3 中禁用区域重定向器 (S3RegionRedirector) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73910120/