繁体   English   中英

无法使用AWS Python Boto3创建S3存储桶(在特定区域)

[英]Unable to Create S3 Bucket(in specific Region) using AWS Python Boto3

我正在尝试使用AWS Python Boto 3创建存储桶。

这是我的代码:

import boto3
response = S3_CLIENT.create_bucket(
  Bucket='symbols3arg',
  CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'}
)
print(response)

我得到以下错误:-

botocore.exceptions.ClientError:调用CreateBucket操作时发生错误(IllegalLocationConstraintException):未指定的位置约束与该请求发送到的特定于区域的终结点不兼容。

发生这种情况是在aws configure期间在s3客户端对象启动中指定其他区域时aws configure了其他区域。

假设我的AWS配置看起来像

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODEXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json

和我创建桶的python脚本

import logging
import boto3
from botocore.exceptions import ClientError


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

create_bucket("test-bucket-in-region","us-west-1")

这将引发以下错误

 ERROR:root:An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The us-west-1 location constraint is incompatible for the region specific endpoint this request was sent to.

在此处输入图片说明 要解决此问题,您需要在s3客户端对象启动中指定区域。 不管aws configure如何,都可以在不同区域工作的示例

import logging
import boto3
from botocore.exceptions import ClientError


def create_bucket(bucket_name, region=None):
    """Create an S3 bucket in a specified region

    If a region is not specified, the bucket is created in the S3 default
    region (us-east-1).

    :param bucket_name: Bucket to create
    :param region: String region to create bucket in, e.g., 'us-west-2'
    :return: True if bucket created, else False
    """

    # Create bucket
    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

create_bucket("my-working-bucket","us-west-1")

创建一个Amazon S3桶

将命令发送到同一区域中的S3:

import boto3

s3_client = boto3.client('s3', region_name='eu-west-1')
response = s3_client.create_bucket(
  Bucket='symbols3arg',
  CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'}
)

您可以尝试以下代码。

import boto3

client = boto3.client('s3',region_name="aws_region_code")

response = client.create_bucket(
    Bucket='string'
)

希望,这可能会有所帮助。

暂无
暂无

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

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