amazon-s3 - 我可以在 boto3 中禁用区域重定向器 (S3RegionRedirector) 吗?

标签 amazon-s3 boto3 botocore

我正在使用 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/

相关文章:

python - 在 dynamodb 中返回具有最大排序键的项目

python - 如何将 botocore1.2.6 升级到 boto3 的 1.4 或更高版本?

python - boto3、aiobotocore、awscli 和 botocore 的版本不兼容;无法解决

django - 如何使用 Django Rest Framework 将上传进度条与 s3 存储桶上的上传同步

python - 如何在 Amazon S3 中复制大于 5 GB 的文件?

amazon-web-services - AWS Lambda 可以访问其他区域的 S3 存储桶吗?

python - Boto3、AWS Lambda - 每次调用的结果复合

python - 从 S3 开始的 Luigi Pipeline

python - 使用 boto3 清空 s3 存储桶的最快方法是什么?

amazon-web-services - 无法通过 AWS 配置文件承担角色