有办法从boto3获取访问密钥和机密密钥吗?

2024-04-27 04:31:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

问题

我想知道有没有办法从boto3获得访问密钥和密钥?例如,如何使用print将它们打印到标准控制台上


Tags: 实例nameimportaws角色s3密钥boto3
1条回答
网友
1楼 · 发布于 2024-04-27 04:31:06

当然有(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)

相关问题 更多 >