boto3 s3连接错误:调用ListBuckets操作时发生错误(SignatureDoesNotMatch)

时间:2018-12-27 13:40:55

标签: python-3.x amazon-s3 boto3

我正在使用boto3软件包从s3集群外部进行连接(即,脚本当前不在AWS“云”中运行,而是从我的MBP连接至相关集群)。我的代码:

s3 = boto3.resource(
    "s3",
    aws_access_key_id=self.settings['CREDENTIALS']['aws_access_key_id'],
    aws_secret_access_key=self.settings['CREDENTIALS']['aws_secret_access_key'],
)

bucket = s3.Bucket(self.settings['S3']['bucket_test'])

for bucket_in_all in boto3.resource('s3').buckets.all():
    if bucket_in_all.name == self.settings['S3']['bucket_test']:
        print ("Bucket {} verified".format(self.settings['S3']['bucket_test']))

现在我收到此错误消息:

botocore.exceptions.ClientError: An error occurred (SignatureDoesNotMatch) when calling the ListBuckets operation

我知道如何检查aws凭证的顺序,并尝试对我的环境变量和~/.aws/credentials进行不同的排列,并且知道应该覆盖我的.py脚本中的凭证,但是我我仍然看到此SignatureDoesNotMatch错误消息。有什么想法我可能会出错吗?我也尝试过:

  # Create a session
    session = boto3.session.Session(
        aws_access_key_id=self.settings['CREDENTIALS']['aws_access_key_id'],
        aws_secret_access_key=self.settings['CREDENTIALS']['aws_secret_access_key'],
        aws_session_token=self.settings['CREDENTIALS']['session_token'],
        region_name=self.settings['CREDENTIALS']['region_name']
    )

s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)

...但是我也看到相同的错误回溯。

1 个答案:

答案 0 :(得分:0)

实际上,这部分由@John Rotenstein和@bdcloud回答,不过我需要更具体一点...

在我的情况下,以下代码不是必需的,并且会导致错误消息:

  # Create a session
    session = boto3.session.Session(
        aws_access_key_id=self.settings['CREDENTIALS']['aws_access_key_id'],
        aws_secret_access_key=self.settings['CREDENTIALS']['aws_secret_access_key'],
        aws_session_token=self.settings['CREDENTIALS']['session_token'],
        region_name=self.settings['CREDENTIALS']['region_name']
    ) 

现在存储在self.settings中的凭据反映了~/.aws/credentials。奇怪的是(就像上周发生逆转一样),我现在可以访问了。可能是笔记本电脑的简单重新启动意味着~/.aws/credentials中的新凭据(因为昨天我再次更新了这些凭据)随后被“接受”。