如何使用 Boto3 获取 AWS Secrets Manager 中所有密钥的列表
问题陈述: 使用 Python 中的 boto3 库获取 AWS Secrets Manager 中所有密钥的列表
解决此问题的方法/算法
步骤 1: 导入 boto3 和 botocore 异常以处理异常。
步骤 2: 此处没有参数。
步骤 3: 使用 boto3 库创建 AWS 会话。确保在默认配置文件中提到了 region_name。如果未提及,则在创建会话时显式传递 region_name。
步骤 4: 为 secretmanager 创建 AWS 客户端。
步骤 5: 调用 list_secrets 函数以检索所有密钥。
步骤 6: 它返回所有密钥的元数据。
步骤 7: 如果获取所有密钥的详细信息时出现问题,则处理通用异常。
示例代码
使用以下代码获取 AWS Secret Manager 中所有密钥的列表:
import boto3
from botocore.exceptions import ClientError
def get_all_secrets():
session = boto3.session.Session()
s3_client = session.client('secretmanager')
try:
response = s3_client.list_secrets()
return response
except ClientError as e:
raise Exception("boto3 client error in get_all_secrets: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_all_secrets: " + e.__str__())
a = get_all_secrets()
for details in a['SecretList']:
print(details['Name'])输出
tests/secrets tests/aws/secrets tests/aws/users
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP