背景
GCPのSecret Managerで設定したシークレットの値を、pythonで取得するスクリプト
実装
from google.cloud import secretmanager
class SecretManagerUtil:
def get_secret(self, project_id: str, secret_id: str) -> str:
client = secretmanager.SecretManagerServiceClient()
name = f"projects/{project_id}/secrets/{secret_id}/versions/latest"
response = client.access_secret_version(request={"name": name})
return response.payload.data.decode("UTF-8")
if __name__ == '__main__'
SecretManagerUtil().get_secret(PROJECT_ID, SECRET_ID)
参考