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

yhqotfr8  于 2023-08-08  发布在  Python
关注(0)|答案(3)|浏览(152)

我正在尝试使用AWS Python Boto 3创建Bucket。
下面是我的代码:

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

字符串
我得到下面的错误:-
botocore.exceptions.ClientError:调用CreateBucket操作时出错(IllegalLocationConstraintException):未指定的位置约束与此请求发送到的区域特定终结点不兼容。

6psbrbz9

6psbrbz91#

aws configure期间,在s3客户端对象初始化中指定不同的区域时,会发生这种情况。
假设我的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")


create-an-amazon-s3-bucket

jmp7cifd

jmp7cifd2#

向同一地域的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'}
)

字符串

83qze16e

83qze16e3#

您可以尝试以下代码:

import boto3

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

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

字符串

相关问题