简体   繁体   English

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

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

I am trying to create bucket using aws python boto 3. 我正在尝试使用AWS Python Boto 3创建存储桶。

Here is my code:- 这是我的代码:

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

I am getting below error:- 我得到以下错误:-

botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to. botocore.exceptions.ClientError:调用CreateBucket操作时发生错误(IllegalLocationConstraintException):未指定的位置约束与该请求发送到的特定于区域的终结点不兼容。

This happens you configured a different region during aws configure in specifying a different region in s3 client object initiation. 发生这种情况是在aws configure期间在s3客户端对象启动中指定其他区域时aws configure了其他区域。

Suppose my AWS config look like 假设我的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

and my python script for creating bucket 和我创建桶的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")

This will throw the below error 这将引发以下错误

 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.

在此处输入图片说明 To solve this issue all you need to specify the region in s3 client object initiation. 要解决此问题,您需要在s3客户端对象启动中指定区域。 A working example in different region regardless of aws configure 不管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 创建一个Amazon S3桶

Send the command to S3 in the same region: 将命令发送到同一区域中的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'}
)

You can try the following code. 您可以尝试以下代码。

import boto3

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

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

Hope, it might helps. 希望,这可能会有所帮助。

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

相关问题 无法使用 boto3 创建 s3 存储桶 - Unable to create s3 bucket using boto3 是否可以使用 AWS Python Boto3 在 us-east-1 中创建 S3 存储桶 - Is it possible to create a S3 Bucket in us-east-1 using AWS Python Boto3 如何使用 python boto3 在 aws S3 中创建文件 - how to create file in aws S3 using python boto3 无法使用Python Boto 3在AWS s3存储桶中上传文件 - unable to upload file in AWS s3 Bucket using Python Boto 3 使用 Boto3 Python 列出 AWS S3 存储桶中的所有对象及其存储 class - List all objects in AWS S3 bucket with their storage class using Boto3 Python 无法使用 python boto3 模块连接到 nexrad aws s3 存储桶 - Cant Connect to nexrad aws s3 bucket using python boto3 module AWS:使用boto3检索具有交叉账户访问权限的S3存储桶的列表的Python脚本 - AWS : Python Script to Retrieve list of S3 bucket having Cross account access using boto3 使用 boto3 和 python 从 S3 存储桶目录中仅读取特定格式的文件 - Reading only specific format files from a S3 bucket dir using boto3 and python 如何使用 Python Boto3 列出和读取 S3 存储桶的特定文件夹中的每个文件 - How to list and read each of the files in specific folder of an S3 bucket using Python Boto3 如何使用条件为 python boto3 代码在 s3 存储桶中创建文件夹 - how to create a folder in s3 bucket by using python boto3 code for with conditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM