本文介绍了有没有办法从boto3获取access_key和secret_key?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动具有IAM角色的EC2实例时,可以在该EC2instance上使用 boto3 ,而不必指定AWS访问和密钥,因为 boto3 会自动读取它们.

When I launch an EC2 instance with an IAM role I can use boto3 on that EC2instance and not have to specify aws access and secret keys because boto3 reads them automatically.

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

问题

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

I'm wondering if there is any way to get the access key and secret key from boto3? For example, how can I print them on to the standard console using print

推荐答案

确定是(文档):

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)

这篇关于有没有办法从boto3获取access_key和secret_key?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:14