首页 > 解决方案 > 使用 sudo python3 但不使用 python3 时导入 aws 凭据时出错

问题描述

我是 aws cli、boto 的初学者,并且正在从事物联网项目。最近遇到这个错误,如果我使用 sudo python3 并尝试连接到 s3,我的连接是经过身份验证的。但如果我只使用 python3 我可以连接。任何想法为什么会发生这种情况?

如果我的脚本需要超级用户权限,我以后会遇到错误吗?我现在通过 USB 连接使用互联网,并且需要超级用户命令来运行我的代码。

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()
>>>

标签: pythonpython-3.xdebianbotosudo

解决方案


我将假设您正在使用凭据文件。如果您在 sudo 下运行 python 脚本,那么它将在 root 帐户下运行,并且不会访问您正在使用的用户下的凭据文件。

一种快速的测试方法是将凭证文件从您的用户帐户复制到根帐户。

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


推荐阅读