当我启动具有 IAM 角色的 EC2 实例时,我可以使用boto3
在该 EC2 实例上,不必指定 aws 访问权限和密钥,因为boto3
自动读取它们 http://boto3.readthedocs.io/en/latest/guide/configuration.html#iam-role.
>>> import boto3
>>> s3 = boto3.resource("s3")
>>> list(s3.buckets.all())[0]
s3.Bucket(name='my-bucket-name')
Question
我想知道是否有任何方法可以从 boto3 获取访问密钥和秘密密钥?例如,如何使用将它们打印到标准控制台上print
肯定有(docs http://boto3.readthedocs.io/en/latest/reference/core/session.html#boto3.session.Session.get_credentials):
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)