使用python在AWS上创建存储桶错误

时间:2018-08-17 10:33:39

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

我想创建一个存储桶以在其中上传一些wav文件。我可以手动创建具有所需位置的存储桶,但是当我尝试使用python编程以创建具有us-west-2位置的存储桶时

session = boto3.Session(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
s3 = session.resource('s3')

s3.create_bucket(Bucket='test-asterisk1', CreateBucketConfiguration={'LocationConstraint': 'eu-central-1'})

我遇到以下错误

Traceback (most recent call last):
  File "create_bucket.py", line 10, in <module>
  s3.create_bucket(Bucket='asterisk1', CreateBucketConfiguration={'LocationConstraint': 'ap-south-1'})
  File "/home/dileep/.local/lib/python2.7/site-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
  File "/home/dileep/.local/lib/python2.7/site-packages/boto3/resources/action.py", line 83, in __call__ response = getattr(parent.meta.client, operation_name)(**params)
  File "/home/dileep/.local/lib/python2.7/site-packages/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
  File "/home/dileep/.local/lib/python2.7/site-packages/botocore/client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)

botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The ap-south-1 location constraint is incompatible for the region specific endpoint this request was sent to.

我在印度IP上试图创建一个“ us-west-2”端点是在制造问题吗?

所以我一次changing location constrain一次

"LocationConstraint": "EU"|"eu-west-1"|"us-west-1"|"us-west-2"|"ap-south-1"|"ap-southeast-1"|"ap-southeast-2"|"ap-northeast-1"|"sa-east-1"|"cn-north-1"|"eu-central-1"

,但是无论我在哪里尝试,都会给我同样的错误。

所以我尝试使用boto而不是boto3进行创建

import boto
from boto.s3.connection import Location
s3 = boto.connect_s3(aws_access_key_id, aws_secret_access_key)
s3.create_bucket('test-asterisk1', location=Location.USWest2)

它引发错误

File "s2t_amazon.py", line 27, in <module>
s3.create_bucket('test-asterisk2', location=Location.USWest2)
File "/home/dileep/.local/lib/python2.7/site-packages/boto/s3/connection.py", line 623, in create_bucket
response.status, response.reason, body)
boto.exception.S3CreateError: S3CreateError: 409 Conflict 
<?xml version="1.0" encoding="UTF-8"?> 
<Error><Code>BucketAlreadyOwnedByYou</Code><Message>Your previous request to create the named bucket succeeded and you already own it. 
</Message><BucketName>test-asterisk2</BucketName> 
<RequestId>EAF26BA152FD20A5</RequestId<HostId>ep0WFZEb1mIjEgbYIY4BGGuOTi5HSutYd3XTKgFjWmRMnGG0ajj5TLF4/t1amJQsOZdZQrqGnoE=</HostId></Error>

我检查了是否创建了存储桶,并且该桶不是由任何方法创建的。谁能建议可能是什么问题?

1 个答案:

答案 0 :(得分:2)

这有效:

import boto3

s3_client = boto3.client('s3', region_name = 'eu-central-1')

s3_client.create_bucket(Bucket='my-bucket', CreateBucketConfiguration={'LocationConstraint': 'eu-central-1'})

要实现的导入是必须将命令发送到创建存储分区的区域。因此,在创建客户端以及创建存储区时,您需要指定区域。