KAROUSHI -Japanese Engineer Blog-

コボルドからドラゴンへ -Kobold to Dragon-

システムエンジニアのブログです。サイト名は「雑魚キャラからボスキャラへレベルアップしたい!」という思いを込めて命名しました。自分はやっとリザードマンになったくらいです。

boto3でS3に接続しようとして"The bucket you are attempting to access must be addressed using the specified endpoint."エラー

目次

概要

boto3でS3に接続しようとして"The bucket you are attempting to access must be addressed using the specified endpoint."のエラーが発生。

環境情報

OS:Linux ip-172-31-28-146 4.4.11-23.53.amzn1.x86_64 #1 SMP Wed Jun 1 22:22:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

pythonPython 2.7.10

ライブラリ:boto3

事象

$ python getObjectFromS3.py

test.test.test.3
Traceback (most recent call last):
File "getObjectFromS3.py", line 13, in <module>
ContentType='text/plane'
File "/usr/local/lib/python2.7/site-packages/boto3/resources/factory.py", line 518, in do_action
response = action(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/usr/lib/python2.7/dist-packages/botocore/client.py", line 258, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/lib/python2.7/dist-packages/botocore/client.py", line 548, 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.

原因

エンドポイントのデフォルトがUSリージョンのため、東京リージョンのバケットにアクセスしようとしてエラー。

対処方法

API gatewayのエンドポイントを指定する。

・ソースを修正する。
s3 = boto3.resource('s3')

s3 = boto3.resource('s3', endpoint_url='<エンドポイント>')