AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK或BatchGetSecretValue
一起使用 CLI
以下代码示例演示如何使用 BatchGetSecretValue
。
- CLI
-
- AWS CLI
-
示例 1:检索按名称列出的一组密钥的密钥值
以下
batch-get-secret-value
示例获取三个密钥的秘密值机密。aws secretsmanager batch-get-secret-value \ --secret-id-list
MySecret1
MySecret2
MySecret3
输出:
{ "SecretValues": [ { "ARN": "arn:aws:secretsmanager:us-west-2:123456789012:secret:MySecret1-a1b2c3", "Name": "MySecret1", "VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLEaaaaa", "SecretString": "{\"username\":\"diego_ramirez\",\"password\":\"EXAMPLE-PASSWORD\",\"engine\":\"mysql\",\"host\":\"secretsmanagertutorial.cluster.us-west-2.rds.amazonaws.com\",\"port\":3306,\"dbClusterIdentifier\":\"secretsmanagertutorial\"}", "VersionStages": [ "AWSCURRENT" ], "CreatedDate": "1523477145.729" }, { "ARN": "arn:aws:secretsmanager:us-west-2:123456789012:secret:MySecret2-a1b2c3", "Name": "MySecret2", "VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLEbbbbb", "SecretString": "{\"username\":\"akua_mansa\",\"password\":\"EXAMPLE-PASSWORD\"", "VersionStages": [ "AWSCURRENT" ], "CreatedDate": "1673477781.275" }, { "ARN": "arn:aws:secretsmanager:us-west-2:123456789012:secret:MySecret3-a1b2c3", "Name": "MySecret3", "VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLEccccc", "SecretString": "{\"username\":\"jie_liu\",\"password\":\"EXAMPLE-PASSWORD\"", "VersionStages": [ "AWSCURRENT" ], "CreatedDate": "1373477721.124" } ], "Errors": [] }
有关更多信息,请参阅 S ecrets Manager 用户指南中的批量检索一组AWS密钥。
示例 2:检索筛选器选择的一组密钥的密钥值
以下
batch-get-secret-value
示例获取您账户中名称中包含的机密值机密。MySecret
按名称筛选区分大小写。aws secretsmanager batch-get-secret-value \ --filters Key="name",Values="MySecret"
输出:
{ "SecretValues": [ { "ARN": "arn:aws:secretsmanager:us-west-2:123456789012:secret:MySecret1-a1b2c3", "Name": "MySecret1", "VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLEaaaaa", "SecretString": "{\"username\":\"diego_ramirez\",\"password\":\"EXAMPLE-PASSWORD\",\"engine\":\"mysql\",\"host\":\"secretsmanagertutorial.cluster.us-west-2.rds.amazonaws.com\",\"port\":3306,\"dbClusterIdentifier\":\"secretsmanagertutorial\"}", "VersionStages": [ "AWSCURRENT" ], "CreatedDate": "1523477145.729" }, { "ARN": "arn:aws:secretsmanager:us-west-2:123456789012:secret:MySecret2-a1b2c3", "Name": "MySecret2", "VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLEbbbbb", "SecretString": "{\"username\":\"akua_mansa\",\"password\":\"EXAMPLE-PASSWORD\"", "VersionStages": [ "AWSCURRENT" ], "CreatedDate": "1673477781.275" }, { "ARN": "arn:aws:secretsmanager:us-west-2:123456789012:secret:MySecret3-a1b2c3", "Name": "MySecret3", "VersionId": "a1b2c3d4-5678-90ab-cdef-EXAMPLEccccc", "SecretString": "{\"username\":\"jie_liu\",\"password\":\"EXAMPLE-PASSWORD\"", "VersionStages": [ "AWSCURRENT" ], "CreatedDate": "1373477721.124" } ], "Errors": [] }
有关更多信息,请参阅 S ecrets Manager 用户指南中的批量检索一组AWS密钥。
-
有关API详细信息,请参阅 “BatchGetSecretValue AWS CLI
命令参考”。
-
- Python
-
- SDK适用于 Python (Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class BatchGetSecretsWrapper: def __init__(self, secretsmanager_client): self.client = secretsmanager_client def batch_get_secrets(self, filter_name): """ Retrieve multiple secrets from AWS Secrets Manager using the batch_get_secret_value API. This function assumes the stack mentioned in the source code README has been successfully deployed. This stack includes 7 secrets, all of which have names beginning with "mySecret". :param filter_name: The full or partial name of secrets to be fetched. :type filter_name: str """ try: secrets = [] response = self.client.batch_get_secret_value( Filters=[{"Key": "name", "Values": [f"{filter_name}"]}] ) for secret in response["SecretValues"]: secrets.append(json.loads(secret["SecretString"])) if secrets: logger.info("Secrets retrieved successfully.") else: logger.info("Zero secrets returned without error.") return secrets except self.client.exceptions.ResourceNotFoundException: msg = f"One or more requested secrets were not found with filter: {filter_name}" logger.info(msg) return msg except Exception as e: logger.error(f"An unknown error occurred:\n{str(e)}.") raise
-
有关API详细信息,请参阅BatchGetSecretValue中的 AWS SDKPython (Boto3) API 参考。
-
操作
GetSecretValue