簡體   English   中英

使用sudo python3而不是python3時導入AWS憑證時出錯

[英]Error importing aws credentials while using sudo python3 but not with python3

我是AWS CLI,Boto的初學者,並且正在從事IoT項目。 最近遇到此錯誤,即如果我使用sudo python3並嘗試連接到s3,則我的連接已通過身份驗證。 但是,如果我只使用python3,我就可以連接。 任何想法為什么會這樣?

如果我需要腳本的超級用戶權限,以后會遇到錯誤嗎? 我現在正在通過USB連接使用Internet,並且需要superuser命令來運行我的代碼。

debian@maraca:~$ sudo python3 
[sudo] password for debian: Python 3.5.3
(default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516] on linux 
Type "help", "copyright", "credits" or "license" for more information.

>>> import boto

>>> s3 = boto.connect_s3()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>

File "/usr/local/lib/python3.5/dist-packages/boto/__init__.py", line 141, in connect_s3

return S3Connection(aws_access_key_id, aws_secret_access_key, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/boto/s3/connection.py", line 194, in __init__

validate_certs=validate_certs, profile_name=profile_name)

File "/usr/local/lib/python3.5/dist-packages/boto/connection.py", line 569, in __init__

host, config, self.provider, self._required_auth_capability())

File "/usr/local/lib/python3.5/dist-packages/boto/auth.py", line 1021, in get_auth_handler

'Check your credentials' % (len(names), str(names)))

boto.exception.NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials



debian@maraca:~$ python3 
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux 
Type "help", "copyright", "credits" or "license" for more information.

>>> import boto

>>> s3 = boto.connect_s3()
>>>

我將假設您正在使用憑據文件。 如果您在sudo下運行python腳本,那么它將在root帳戶下運行,並且不會訪問您所使用的用戶下的憑據文件。

一種快速的測試方法是將憑據文件從您的用戶帳戶復制到根帳戶。

供參考https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

好的,我想我已經找到問題了。 我是對的,您的憑據位於〜/ .aws / config中,而不是〜/ .aws / credentials中? 如果是這樣,aws-cli將可以工作,但由於該文件假定不包含敏感選項,因此boto將不會使用它們:

http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html http://boto.readthedocs.org/en/latest/boto_config_tut.html#details

這是我指的評論:

AWS CLI還將從配置文件讀取憑證。 如果要將所有配置文件設置保留在一個文件中,則可以。 如果配置文件的兩個位置都存在憑據(例如您使用aws configure更新配置文件的密鑰),則憑據文件中的密鑰將占優。

如果您除了使用AWS CLI之外還使用其中一種SDK,則可能會注意到其他警告(如果憑據未存儲在其自己的文件中)。 我將憑據作為〜/ .aws / config的一部分,並刪除了〜/ .aws / credentials,並重現了相同的錯誤。

作為此修復程序的一部分,我要求使用--aws-region或AWS_REGION env變量提供有效的aws-region。 我認為應該足以使這種情況不再發生。

暫無
暫無

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

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