You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 16:38:14