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

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 configure

之后我手动输入:

AWS Access Key ID [None]: xxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxx
Default region name [None]: us-east-1
Default output format [None]: just hit enter

从那以后,它对我起作用了

其他回答

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

终端类型:-

aws configure

然后填写密钥和区域。

我也有同样的问题,它可以通过在主目录中创建配置和凭据文件来解决。下面展示了我解决这个问题的步骤。

创建一个配置文件:

touch ~/.aws/config

在那个文件里,我输入了区域

[default]
region = us-west-2

然后创建证书文件:

touch ~/.aws/credentials

然后输入你的凭证

[Profile1]
aws_access_key_id = XXXXXXXXXXXXXXXXXXXX 
aws_secret_access_key = YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

设置好所有这些后,然后我的python文件连接桶。运行此文件将列出所有内容。

import boto3
import os

os.environ['AWS_PROFILE'] = "Profile1"
os.environ['AWS_DEFAULT_REGION'] = "us-west-2"

s3 = boto3.client('s3', region_name='us-west-2')
print("[INFO:] Connecting to cloud")

# Retrieves all regions/endpoints that work with S3

response = s3.list_buckets()
print('Regions:', response)

你也可以参考以下链接:

带有Python Boto3库的Amazon S3 Boto 3文档 Boto3: Amazon S3 as Python Object Store

这些指令适用于具有AWS单一用户配置文件的windows机器。确保你的~/。Aws /凭证文件如下所示

[profile_name]
aws_access_key_id = yourAccessId
aws_secret_access_key = yourSecretKey

我必须将AWS_DEFAULT_PROFILEenvironment变量设置为在凭据中找到的profile_name。 然后我的蟒蛇就能连接了。从这里开始

import boto3

# Let's use Amazon S3
s3 = boto3.resource('s3')

# Print out bucket names
for bucket in s3.buckets.all():
    print(bucket.name)

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

确保你的~/。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交互性配置。