使用 client.get_caller_identity()[ "Account" 打印访问密钥和密钥 ]



我正在尝试打印访问密钥和密钥,以检查我是否能够连接到s3 bucket。我创建了一个/我有凭据的aws目录。更新boto3后,错误得到了更改。我已经在下面更新了。

代码:

import boto3
session = boto3.Session()
credentials = session.get_credentials()
credentials = credentials.get_frozen_credentials()
ACCESS_KEY = credentials.access_key
SECRET_KEY = credentials.secret_key
client = boto3.client("sts", aws_access_key_id=ACCESS_KEY, aws_secret_key=SECRET_KEY)
account_id = client.get_caller_identity()["Account"]
print(account_id)

.aws/凭证:

[default]
aws_access_key_id = 'access_key'
aws_secret_access_key = 'secret_key'

有了这些,我得到了一些错误:

ACCESS_KEY = credentials.access_key
AttributeError: 'NoneType' object has no attribute 'access_key'

我不确定是我在这里遗漏了什么,还是我的代码错了。

您似乎在使用旧版本的boto3库,这适用于新版本。尝试更新boto3版本。

我正在使用boto3 1.12.44。

现在解决了一个问题:https://github.com/boto/boto3/issues/590

设置AWS凭据:https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

最新更新