python - 使用 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()
>>>
解决方案
我将假设您正在使用凭据文件。如果您在 sudo 下运行 python 脚本,那么它将在 root 帐户下运行,并且不会访问您正在使用的用户下的凭据文件。
一种快速的测试方法是将凭证文件从您的用户帐户复制到根帐户。
供参考https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
推荐阅读
- kubernetes - 如何在 Azure 管道 (VSTS) 中使用 Deploy to Kubernetes 任务的输出变量
- swift - 使用堆栈和导航栏从 AppDelegate 推送到 ViewController
- android - SharedTransitionElement 动画中的 OutOfMemoryError
- r - 通过用其平均值或中值替换每个箱来去除 R 中具有相等深度分箱的噪声
- java - 我们可以创建具有通用返回类型的方法吗?这如何评估在编译时返回哪种类型?
- excel - VBA - 将文件转换为csv格式后缺少完成0的代码
- django - 使用 Django Rest Framework 将输入文件从 Vue.js 发送到 Django 时出现问题
- python - for循环删除不在python数组中的值
- pip - “pip install tox”给出找到现有安装:filelock 2.0.12
- python - 来自相机的 Python OpenCV 流式传输 - 多线程、时间戳