簡體   English   中英

NoCredentialsError:無法找到憑據 - python 模塊 boto3

[英]NoCredentialsError : Unable to locate credentials - python module boto3

我在python虛擬環境( virtualenv )中運行django django網站由來自亞馬遜 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

我想出了我收到錯誤的原因。 我發布答案以防萬一其他人遇到此問題。

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

django 應用程序在用戶 www-data(apache2) 下運行。 當我使用aws configure配置我的憑據時,我的設置存儲在.aws/config文件中。

現在這就是問題所在。

.aws/configure文件存儲在我的主目錄中,而不是在www-data (apache2) 的主目錄中,默認情況下是/var/www 因此,當 django 應用程序調用 boto3 模塊時,該模塊正在/var/www/.aws/config查找配置文件,但我的文件在/home/ubuntu/.aws/config

只需將相關文件復制到/var/www/為我解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM