如何使用 Boto3 获取 AWS Secrets Manager 中所有密钥的列表


问题陈述: 使用 Python 中的 boto3 库获取 AWS Secrets Manager 中所有密钥的列表

解决此问题的方法/算法

  • 步骤 1: 导入 boto3botocore 异常以处理异常。

  • 步骤 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

更新于:2021年4月16日

2K+ 次浏览

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.