Boto3错误:botocore.exceptions.NoCredentialsError:无法找到凭据 [英] Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials

查看:909
本文介绍了Boto3错误:botocore.exceptions.NoCredentialsError:无法找到凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我简单地运行以下代码时,总是会出现此错误.

When I simply run the following code, I always gets this error.

s3 = boto3.resource('s3')
    bucket_name = "python-sdk-sample-%s" % uuid.uuid4()
    print("Creating new bucket with name:", bucket_name)
    s3.create_bucket(Bucket=bucket_name)

我的凭证文件已保存在

C:\Users\myname\.aws\credentials,Boto应该从那里读取我的凭据.

C:\Users\myname\.aws\credentials, from where Boto should read my credentials.

我的设置是否错误?

这是boto3.set_stream_logger('botocore', level='DEBUG')的输出.

2015-10-24 14:22:28,761 botocore.credentials [DEBUG] Skipping environment variable credential check because profile name was explicitly set.
2015-10-24 14:22:28,761 botocore.credentials [DEBUG] Looking for credentials via: env
2015-10-24 14:22:28,773 botocore.credentials [DEBUG] Looking for credentials via: shared-credentials-file
2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: config-file
2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: ec2-credentials-file
2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: boto-config
2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: iam-role

推荐答案

尝试手动指定键

    s3 = boto3.resource('s3',
         aws_access_key_id=ACCESS_ID,
         aws_secret_access_key= ACCESS_KEY)

请确保出于安全方面的考虑,不要在代码中直接包含ACCESS_ID和ACCESS_KEY. 考虑使用环境配置,并将其注入@Tiger_Mike建议的代码中.

Make sure you don't include your ACCESS_ID and ACCESS_KEY in the code directly for security concerns. Consider using environment configs and injecting them in the code as suggested by @Tiger_Mike.

对于Prod环境,请考虑使用旋转访问键: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_RotateAccessKey

For Prod environments consider using rotating access keys: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_RotateAccessKey

这篇关于Boto3错误:botocore.exceptions.NoCredentialsError:无法找到凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆