如何在Python中获取Google Secret Manager的最新版本ID
自动获取Google Secret Manager最新版本密钥的实现方法
你可以直接使用latest作为版本标识符替代具体数字version_id,Google Secret Manager会自动识别并返回该密钥的最新版本内容,无需手动修改版本号。
修改后的代码:
from google.cloud import secretmanager project_id = "xxxxxx" secret_id = "password" client = secretmanager.SecretManagerServiceClient() # 用latest替代具体版本号,自动拉取最新密钥 secret_detail = f"projects/{project_id}/secrets/{secret_id}/versions/latest" response = client.access_secret_version(request={"name": secret_detail}) data = response.payload.data.decode("UTF-8") password = data
关键改动说明:
- 移除了手动指定的
version_id = '2',直接在资源路径中使用latest - 每次执行脚本时,Secret Manager会自动返回当前处于
ENABLED状态的最新密钥版本,完全适配密码自动轮换的场景
内容的提问来源于stack exchange,提问作者PopularDuck
相关产品推荐
相关产品推荐

