Warm tip: This article is reproduced from stackoverflow.com, please click
boto debian python python-3.x sudo

Error importing aws credentials while using sudo python3 but not with python3

发布于 2020-03-27 10:19:43

I'm a beginner to aws cli, boto and am working on an IoT project. Recently encountered this error that if I use sudo python3 and try to connect to s3, my connection is authenticated. But if I just use python3 I'm able to connect. Any ideas why this is happening?

Will I run into errors later if I need superuser permissions for my script? I am using internet over USB connection right now, and need the superuser command to run my code.

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()
>>>
Questioner
utsav22g
Viewed
104
ankur singh 2019-07-11 21:47

Ok, I think I've found the problem. I am right thinking your credentials are located in ~/.aws/config instead of ~/.aws/credentials? if that it the case, aws-cli will work, but because that file is suppose to not contain sensitive options, boto will not use them:

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

This is the comment I'm referring to:

The AWS CLI will also read credentials from the config file. If you want to keep all of your profile settings in a single file, you can. If there are ever credentials in both locations for a profile (say you used aws configure to update the profile's keys), the keys in the credentials file will take precendence.

If you use one of the SDKs in addition to the AWS CLI, you may notice additional warnings if credentials are not stored in their own file. I've put my credentials as part of ~/.aws/config and remove my ~/.aws/credentials and I've reproduce the same error you got.

As part of this fix I've make a requirement to provide a valid aws-region using --aws-region or AWS_REGION env variable. I think that should be enough in order to make this not happen again.