简体   繁体   English

是否可以使用 AWS Python Boto3 在 us-east-1 中创建 S3 存储桶

[英]Is it possible to create a S3 Bucket in us-east-1 using AWS Python Boto3

Documentation 文档

import logging
import boto3
from botocore.exceptions import ClientError


def create_bucket(bucket_name, region=None):
    try:
        if region is None:
            s3_client = boto3.client('s3')
            s3_client.create_bucket(Bucket=bucket_name)
        else:
            s3_client = boto3.client('s3', region_name=region)
            location = {'LocationConstraint': region}
            s3_client.create_bucket(Bucket=bucket_name,
                                    CreateBucketConfiguration=location)
    except ClientError as e:
        logging.error(e)
        return False
    return True

Testing:测试:

create_bucket('test', 'us-west-2') Works as expected -> Please select a different name and try again create_bucket('test', 'us-west-2')按预期工作 -> 请选择其他名称并重试

create_bucket('test') The unspecified location constraint is incompatible for the region specific endpoint this request was sent to. create_bucket('test')未指定的位置约束与此请求发送到的区域特定端点不兼容。

create_bucket('test', 'us-east-1') The us-east-1 location constraint is incompatible for the region specific endpoint this request was sent to. create_bucket('test', 'us-east-1') us-east-1 位置约束与此请求发送到的区域特定端点不兼容。

What did I miss?我错过了什么?

The problem is in the error message .问题出在error message中。

Obviously, the bucket with the name test is already taken, but for some reason, instead of saying Please select a different name and try again , we see a message that misleads us.显然,名称为test的存储桶已经被占用,但由于某种原因,我们看到了一条误导我们的消息,而不是说Please select a different name and try again

So the solution is simple - use a unique name for the bucket .所以解决方案很简单—— use a unique name for the bucket

I hope AWS will fix the error message ASAP but it's not a blocker for us anyway我希望 AWS 会尽快修复错误消息,但无论如何这对我们来说并不是一个障碍

This creates a bucket in us-east-1 :这会在us-east-1中创建一个存储桶:

s3_client = boto3.client('s3', region_name='us-east-1')
s3_client.create_bucket(Bucket='unique-name-here')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM