boto3凭据文件的格式和位置

时间:2017-09-14 21:24:18

标签: python-3.x amazon-web-services boto3

我刚刚开始使用boto3并尝试了以下代码:

import boto3
boto3.session.Session(profile_name='Credentials')
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)

如果我在〜/ .aws / credentials [default]中命名该部分,它可以正常工作,但如果我将其命名为其他内容,例如[Credentials]并像我一样指定profile_name,则会失败

botocore.exceptions.NoCredentialsError: Unable to locate credentials

我希望能够在凭证文件中指定不同的配置文件,但我无法解决此错误。有些人回答了这个问题,说该部分必须是[默认],但这可能不对。

1 个答案:

答案 0 :(得分:3)

您错过了设置会话变量并在该会话实例上调用资源。

import boto3
session = boto3.session.Session(profile_name='Credentials')
s3 = session.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)

同时验证字符串'Credentials'是否与〜/ .aws / credentials中的[Credentials]完全匹配