【发布时间】:2015-11-21 15:29:28
【问题描述】:
以下代码在本地工作并将文件从目录上传到 S3。它使用 Boto3 和 Python 3。
s3 = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_ACCESS_KEY_SECRET)
bucket = s3.Bucket(bucket_name)
uploadFileNames = []
for (sourceDir, dirname, filenames) in os.walk(sourceDir):
for filename in filenames:
bucket.put_object(Key=filename, Body=open("{}{}".format(sourceDir, filename), "rb"))
break
我的问题是当我在生产服务器 (Ubuntu) 上运行相同的代码时出现以下错误,为什么?
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.4/site-packages/botocore/client.py", line 335, in _make_api_call
raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (PermanentRedirect) when calling the PutObject operation: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
此代码在我的 Mac 本地再次有效,它仅在我的 Ubuntu 服务器上出现此错误。
【问题讨论】:
-
该错误让我想起了我尝试从错误区域访问存储桶的情况。也许在两台机器上运行
aws configure并检查您是否具有相同的默认区域? -
@JohnRotenstein 我不想像那样在 Ubuntu 机器上配置 AWS。我可以在 Boto3 中以编程方式设置区域吗?存储桶位于欧盟西部地区。
-
@JohnRotenstein 你是对的!!!!我解决了这个设置
boto3.setup_default_session(region_name='eu-west-1')。您能否发布一个答案以便其他人受益并且我可以接受?
标签: python python-3.x amazon-web-services amazon-s3 boto