【问题标题】:Failing to create s3 buckets in specific regions无法在特定区域创建 s3 存储桶
【发布时间】:2020-05-27 14:06:20
【问题描述】:

我正在尝试使用 python 中的 boto3 在 AWS 的每个区域中创建一个 s3 存储桶,但我未能在 4 个区域(af-south-1、eu-south-1、ap-east- 1 & me-south-1)

我的python代码:

def create_bucket(name, region):
    s3 = boto3.client('s3')
    s3.create_bucket(Bucket=name, CreateBucketConfiguration={'LocationConstraint': region})

我得到的例外:

botocore.exceptions.ClientError: An error occurred (InvalidLocationConstraint) when calling the CreateBucket operation: The specified location-constraint is not valid

我可以从 aws 网站在这些区域中创建存储桶,但这对我不利,所以我尝试直接从其余 API 中创建它,而不使用 boto3。

网址:bucket-name.s3.amazonaws.com

正文:

<?xml version="1.0" encoding="UTF-8"?>
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
   <LocationConstraint>eu-south-1</LocationConstraint>
</CreateBucketConfiguration>

但响应类似于异常:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>InvalidLocationConstraint</Code>
    <Message>The specified location-constraint is not valid</Message>
    <LocationConstraint>eu-south-1</LocationConstraint>
    <RequestId>********</RequestId>
    <HostId>**************</HostId>
</Error>

有谁知道为什么我可以从网站手动操作,但不能从 python 操作?

【问题讨论】:

  • 您使用区域 S3 端点,还是仅使用全球旧端点“s3.amazonaws.com”?对于虚拟主机,旧端点仅适用于旧区域,不适用于新区域。也许 REST API 也一样?
  • 你是对的!有什么方法可以让 boto3 使用新 URL 而不是旧 URL?

标签: python amazon-web-services rest amazon-s3 boto3


【解决方案1】:

您的代码失败的区域是相对较新的区域,您需要先选择加入才能使用它们,请参阅此处Managing AWS Regions

【讨论】:

  • 您可以使用 boto3 ec2 客户端来找出哪些区域在没有选择加入的情况下可用:请参阅stackoverflow.com/questions/38451032/…
  • 我已经启用了这些区域,我可以在那里打开 ec2/rds 实例,只是不能从代码创建 s3 存储桶(我可以从 s3 控制台)。使用 ec2 的描述区域在这些区域上给了我"OptInStatus": "opted-in"
  • 不知您是否需要在s3 = boto3.client('s3', region_name=region) 中指定区域以将CreateBucket 请求发送到该特定区域,而不是一般的S3 端点?
【解决方案2】:

较新的 AWS 区域仅支持区域端点。因此,如果在其中一个区域中创建存储桶,则需要创建区域端点。

由于我在多个区域中创建存储桶,因此我通过为每个区域创建客户端的新实例来设置端点。 (这是在 Node.js 中,但仍应与 boto3 一起使用)

client = boto3.client('s3', region_name='region')

在 Node.js 上看到同样的问题 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 2021-07-03
    • 2015-11-30
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2014-02-16
    相关资源
    最近更新 更多