如何使用 Boto3 为 DynamoDB 修复 "NoCredentialsError"

how to fix "NoCredentialsError" with Boto3 for DynamoDB

我目前正在使用 Flask 和 DynamoDB 创建一个网络应用程序。

但是,我 运行 目前遇到了一个非常严重的 运行ge 错误,我希望有人可以帮助我。

目前,当我 运行 Pycharm 中的网络应用程序时,一切正常。 我能够从 DynamoDB 请求数据,没有任何问题或错误。

然而,当我 运行 在 Ubuntu 上与 Python3 完全相同的文件时,我突然遇到了“NoCredentialsError”。 当我在 Pycharm 上 运行 时,完全相同的文件工作正常,但出于某种原因,只有当我尝试在 Ubuntu.

中测试它时,它才会给我这个错误

导致此错误的函数很简单:


def selectItem(PK, SK):

    dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
    
    table = dynamodb.Table('testDB')

    response = table.get_item(
        Key={
            'PK': PK,
            'SK': SK
        }
    )
    return response

我将 table 和区域编码只是为了方便上下文并在测试时保持简单。

这是完整的追溯以获取更多详细信息:

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 2095, in call return self.wsgi_app(environ, start_response)

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 2080, in wsgi_app response = self.handle_exception(e)

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 2077, in wsgi_app response = self.full_dispatch_request()

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 1525, in full_dispatch_request rv = self.handle_user_exception(e)

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 1523, in full_dispatch_request rv = self.dispatch_request()

File "/home/testcode/.local/lib/python3.8/site-packages/flask/app.py", line 1509, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

File "/mnt/c/Users/Test/Desktop/Coding/TestCode/mainapp/webapp.py", line 139, in login session["farm"] = DynamoDB.selectItem("Users", session["email"])

File "/mnt/c/Users/Test/Desktop/Coding/TestCode/mainapp/DataBase/DynamoDB.py", line 147, in selectItem response = table.get_item(

File "/home/testcode/.local/lib/python3.8/site-packages/boto3/resources/factory.py", line 580, in do_action response = action(self, *args, **kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/boto3/resources/action.py", line 88, in call response = getattr(parent.meta.client, operation_name)(*args, **params)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/client.py", line 415, in _api_call return self._make_api_call(operation_name, kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/client.py", line 731, in _make_api_call http, parsed_response = self._make_request(

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/client.py", line 751, in _make_request return self._endpoint.make_request(operation_model, request_dict)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 107, in make_request return self._send_request(request_dict, operation_model)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 180, in _send_request request = self.create_request(request_dict, operation_model)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/endpoint.py", line 120, in create_request self._event_emitter.emit(event_name, request=request,

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/hooks.py", line 358, in emit return self._emitter.emit(aliased_event_name, **kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/hooks.py", line 229, in emit return self._emit(event_name, kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/hooks.py", line 212, in _emit response = handler(**kwargs)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/signers.py", line 95, in handler return self.sign(operation_name, request)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/signers.py", line 167, in sign auth.add_auth(request)

File "/home/testcode/.local/lib/python3.8/site-packages/botocore/auth.py", line 401, in add_auth raise NoCredentialsError() botocore.exceptions.NoCredentialsError: Unable to locate credentials

你的 ubuntu 机器上有你的 aws 凭据吗?它应该在 ~/.aws/credentials

寻找它们