有没有办法从boto3获取access_key和secret_key?

时间:2016-12-21 19:32:29

标签: python amazon-web-services boto3

当我启动具有IAM角色的EC2实例时,我可以在该EC2实例上使用boto3,而不必指定aws访问权限和密钥,因为boto3 reads them automatically

>>> import boto3
>>> s3 = boto3.resource("s3")
>>> list(s3.buckets.all())[0]
s3.Bucket(name='my-bucket-name')

问题

我想知道是否有办法从boto3获取访问密钥和密钥?例如,如何使用print

将它们打印到标准控制台

1 个答案:

答案 0 :(得分:12)

肯定有(docs):

from boto3 import Session

session = Session()
credentials = session.get_credentials()
# Credentials are refreshable, so accessing your access key / secret key
# separately can lead to a race condition. Use this to get an actual matched
# set.
current_credentials = credentials.get_frozen_credentials()

# I would not recommend actually printing these. Generally unsafe.
print(current_credentials.access_key)
print(current_credentials.secret_key)
print(current_credentials.token)