当我简单地运行下面的代码时,我总是得到这个错误。

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\证书,从那里Boto应该读我的证书。

我的设置错了吗?

下面是boto3的输出。set_stream_logger (botocore,级别=“调试”)。

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

当前回答

我有同样的问题,并发现我的~/。Aws /凭证文件错误。

它处理了一个包含以下内容的文件:

[default]
aws_access_key_id=XXXXXXXXXXXXXX
aws_secret_access_key=YYYYYYYYYYYYYYYYYYYYYYYYYYY

注意,必须有一个配置文件名称“[default]”。一些官方文件提到了一个名为“[凭证]”的配置文件,这对我来说并不适用。

其他回答

确保你的~/。Unix中的aws/credentials文件是这样的:

[MyProfile1]
aws_access_key_id = yourAccessId
aws_secret_access_key = yourSecretKey

[MyProfile2]
aws_access_key_id = yourAccessId
aws_secret_access_key = yourSecretKey

你的Python脚本应该是这样的,它可以工作:

from __future__ import print_function
import boto3
import os

os.environ['AWS_PROFILE'] = "MyProfile1"
os.environ['AWS_DEFAULT_REGION'] = "us-east-1"

ec2 = boto3.client('ec2')

# Retrieves all regions/endpoints that work with EC2
response = ec2.describe_regions()
print('Regions:', response['Regions'])

来源:https://boto3.readthedocs.io/en/latest/guide/configuration.html交互性配置。

我在一家大公司工作,遇到过同样的错误,但我需要一个不同的工作方式。我的问题与代理设置有关。我已经设置了代理,所以在我能够让一切正常工作之前,我需要将我的no_proxy设置为AWS白名单。你也可以在bash脚本中设置它,如果你不想用操作系统设置混淆你的Python代码。

Python:

import os
os.environ["NO_PROXY"] = "s3.amazonaws.com"

Bash:

export no_proxy = "s3.amazonaws.com"

编辑:以上假设美国东部S3地区。对于其他区域:使用s3.[region].amazonaws.com,其中区域类似于us-east-1或us-west-2

如果您正在寻找另一种方法,请尝试使用 AmazonCLI

终端类型:-

aws configure

然后填写密钥和区域。

如果在~/中有多个aws配置文件。aws /凭证等……

[Profile 1]
aws_access_key_id = *******************
aws_secret_access_key = ******************************************
[Profile 2]
aws_access_key_id = *******************
aws_secret_access_key = ******************************************

遵循两个步骤:

在终端中使用export AWS_DEFAULT_PROFILE=Profile 1命令创建一个您想使用的默认值。 请确保在使用boto3或打开编辑器的同一终端上运行上述命令。[理解以下场景]

场景:

如果有两个终端,分别是t1和t2。 然后在t1中运行export命令,然后从t2中打开JupyterLab或任何其他命令,您将得到NoCredentialsError: Unable to locate credentials错误。

解决方案:

在t1中运行export命令,然后从同一终端t1打开JupyterLab或任何其他命令。

如果您确定正确配置了aws,只需确保项目的用户可以从./aws读取数据,或者只是以根用户身份运行项目