KAROUSHI -Japanese Engineer Blog-

KAROUSHI -Japanese Engineer Blog-

Web、ネットワーク構築関連で仕事している、器用貧乏エンジニア。最近はAWS関連の勉強をしています。

"botocore.exceptions.NoCredentialsError: Unable to locate credentials" 認証情報エラーの対処方法

目次

 

概要

boto3でpythonコードを記述して実行しようとしたらエラーが発生。

環境情報

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

事象

getObjectFromS3.pyのソース

----------------------------
import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('test.test.test.3')
print(bucket.name)

obj = bucket.Object('test.txt')
body ="Helloworld."

response = obj.put(
Body=body.encode('utf-8'),
ContentEncoding='utf-8',
ContentType='text/plane'
)
----------------------------

上記ソースを実行
$ 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 537, in _make_api_call
operation_model, request_dict)
File "/usr/lib/python2.7/dist-packages/botocore/endpoint.py", line 117, in make_request
return self._send_request(request_dict, operation_model)
File "/usr/lib/python2.7/dist-packages/botocore/endpoint.py", line 142, in _send_request
request = self.create_request(request_dict, operation_model)
File "/usr/lib/python2.7/dist-packages/botocore/endpoint.py", line 126, in create_request
operation_name=operation_model.name)
File "/usr/lib/python2.7/dist-packages/botocore/hooks.py", line 227, in emit
return self._emit(event_name, kwargs)
File "/usr/lib/python2.7/dist-packages/botocore/hooks.py", line 210, in _emit
response = handler(**kwargs)
File "/usr/lib/python2.7/dist-packages/botocore/signers.py", line 90, in handler
return self.sign(operation_name, request)
File "/usr/lib/python2.7/dist-packages/botocore/signers.py", line 124, in sign
signer.add_auth(request=request)
File "/usr/lib/python2.7/dist-packages/botocore/auth.py", line 626, in add_auth
raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials

原因

AWSに繋ぐための認証情報が設定されていない。

対処方法

"aws configure"コマンドを使ってAWSの認証情報を設定する。

AWSの認証情報を設定する。
$ aws configure
AWS Access Key ID [None]:<アクセスキー>
AWS Secret Access Key [None]:<シークレットキー>
Default region name [None]:<リージョン>
Default output format [None]:<json/table/textのどれか>