如何使用 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