文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
BatchGetSecretValue
搭配 a AWS SDK 或 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": [] }
如需詳細資訊,請參閱 Secrets 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": [] }
如需詳細資訊,請參閱 Secrets Manager 使用者指南中的擷取批次中的一組秘密。 AWS
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 BatchGetSecretValue
。
-
- Python
-
- SDK for Python (Boto3)
-
注意
還有更多 on 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 SDK for Python (Boto3) Word 參考中的 API。
-
動作
GetSecretValue