【问题标题】:NoCredentialsError : Unable to locate credentials - python module boto3NoCredentialsError:无法找到凭据 - python 模块 boto3
【发布时间】:2017-08-21 12:51:59
【问题描述】:

我在python 虚拟环境(virtualenv) 中运行djangodjango 网站由来自亚马逊 ec2 实例(ubuntu 16.04)的apache2 提供服务。我使用boto3 模块写入亚马逊s3。

我安装了 awscli 并运行了 aws configure 并正确设置了我的 aws 访问密钥。 (我知道我配置正确,因为$ aws s3 ls 返回了我的 s3 存储桶的正确列表。)

但是,当我尝试从 django 应用程序将一些对象写入 s3 时,它无法产生标题中描述的错误。

我最近搬到了一个新实例并开始使用 python 虚拟环境。在此之前,它曾经运行良好。我已经阅读了关于 SO 的问题和来自 aws 的文档。下面是堆栈跟踪。

Environment:


Request Method: POST
Request URL: http://*******/product/4

Django Version: 1.10.6
Python Version: 3.5.2
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'abc.apps.abcdirectConfig')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')



Traceback:

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  42.             response = get_response(request)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/home/ubuntu/abcdirect/abcdirect/views.py" in view_product
  385.             s3.Bucket('abccms').put_object(Key=s3_file_name, Body=s3_file_data)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/boto3/resources/factory.py" in do_action
  520.                 response = action(self, *args, **kwargs)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/boto3/resources/action.py" in __call__
  83.         response = getattr(parent.meta.client, operation_name)(**params)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/client.py" in _api_call
  253.             return self._make_api_call(operation_name, kwargs)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/client.py" in _make_api_call
  530.                 operation_model, request_dict)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/endpoint.py" in make_request
  141.         return self._send_request(request_dict, operation_model)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/endpoint.py" in _send_request
  166.         request = self.create_request(request_dict, operation_model)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/endpoint.py" in create_request
  150.                                      operation_name=operation_model.name)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/hooks.py" in emit
  227.         return self._emit(event_name, kwargs)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/hooks.py" in _emit
  210.             response = handler(**kwargs)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/signers.py" in handler
  90.         return self.sign(operation_name, request)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/signers.py" in sign
  147.             auth.add_auth(request)

File "/home/ubuntu/.virtualenv/lib/python3.5/site-packages/botocore/auth.py" in add_auth
  679.             raise NoCredentialsError

Exception Type: NoCredentialsError at /product/4
Exception Value: Unable to locate credentials

【问题讨论】:

  • 检查~/.aws/credentials 上的凭据是否设置正确。
  • @juliansalasyes 他们设置正确。我可以从命令行连接到 s3 并上传/下载文件。只是不是通过 boto3 来自 django 应用程序。

标签: django amazon-web-services amazon-s3 boto3


【解决方案1】:

我找到了错误的原因。我发布答案以防其他人遇到此问题。

tl;dr:aws 配置文件不在 apache 的主目录中

django 应用程序在用户 www-data(apache2) 下运行。当我使用aws configure 配置我的凭据时,我的设置存储在.aws/config 文件中。

这就是问题所在。

.aws/configure 文件存储在 my 主目录中,而不是 www-data(apache2) 的主目录中,默认为 /var/www。因此,当 django 应用调用 boto3 模块时,该模块在 /var/www/.aws/config 中查找配置文件,但我的文件在 /home/ubuntu/.aws/config 中。

只需将相关文件复制到/var/www/ 即可解决我的问题。

【讨论】:

  • 我也面临同样的问题。我已经应用了您的解决方案并将两个配置文件移动到 /var/www 文件夹,但对我来说它仍然显示相同的错误。我正在使用flask + apache 来运行应用程序。
  • @Ajay 确保文件现在位于 /var/www/.aws/config 中。并且男性肯定 apache 可以读取文件。将.aws目录和里面的config文件的权限改成其他人可读,或者把所有者改成apache。
  • 我已将这两个文件的所有者从 root 设置为 ubuntu,因为 apache 不是我的 ec2 机器中的有效用户。执行这些操作后,错误仍然存​​在。
  • 确保 'www-data' 具有对该文件的读取权限。 Apache 以“www-data”用户身份运行( cat /etc/passwd 列出系统中的所有用户)
猜你喜欢
  • 1970-01-01
  • 2022-12-16
  • 2021-09-13
  • 2016-01-22
  • 2022-07-20
  • 2020-04-14
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
相关资源
最近更新 更多