【问题标题】:PermanentRedirect when calling the PutObject operation调用 PutObject 操作时的 PermanentRedirect
【发布时间】: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


【解决方案1】:

错误说:

调用 PutObject 操作时发生错误 (PermanentRedirect):您尝试访问的存储桶必须使用指定的端点来寻址。请将所有未来请求发送到此端点

当您使用的 Amazon S3 存储桶与创建 Amazon S3 客户端的位置位于不同的区域时,通常会发生这种情况。

例如,存储桶位于us-west-2,但客户端是为ap-southeast-2 创建的。

您可以指定区域via a credentials file,或在创建客户端对象时传递region_name。也可以在boto3.setup_default_session() 中定义默认区域。

【讨论】:

    猜你喜欢
    • 2017-01-01
    • 2022-11-08
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2018-07-12
    • 2018-12-09
    • 2020-04-02
    相关资源
    最近更新 更多