【问题标题】:How to Add Environment Variables to Google App Engine如何将环境变量添加到 Google App Engine
【发布时间】:2020-08-04 18:21:46
【问题描述】:
我已将我的 Django 项目部署到 Google App Engine,我需要添加环境变量。
文档说要将它们添加到 app.yaml,但这似乎是一种不好的做法,因为 app.yaml 应该在您的 git 存储库中。
有什么方法可以像在 Cloud Run > Services > Variables & Secrets 中添加环境变量一样向 App Engine 添加环境变量?
【问题讨论】:
标签:
google-app-engine
environment-variables
【解决方案1】:
Google Secret Manager 于今年春季推出:
def create_secret(project_id, secret_id):
"""
Create a new secret with the given name. A secret is a logical wrapper
around a collection of secret versions. Secret versions hold the actual
secret material.
"""
# Import the Secret Manager client library.
from google.cloud import secretmanager
# Create the Secret Manager client.
client = secretmanager.SecretManagerServiceClient()
# Build the resource name of the parent project.
parent = client.project_path(project_id)
# Create the secret.
response = client.create_secret(parent, secret_id, {
'replication': {
'automatic': {},
},
})
# Print the new secret name.
print('Created secret: {}'.format(response.name))
def access_secret_version(project_id, secret_id, version_id):
"""
Access the payload for the given secret version if one exists. The version
can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
"""
# Import the Secret Manager client library.
from google.cloud import secretmanager
# Create the Secret Manager client.
client = secretmanager.SecretManagerServiceClient()
# Build the resource name of the secret version.
name = client.secret_version_path(project_id, secret_id, version_id)
# Access the secret version.
response = client.access_secret_version(name)
# Print the secret payload.
#
# WARNING: Do not print the secret in a production environment - this
# snippet is showing how to access the secret material.
payload = response.payload.data.decode('UTF-8')
print('Plaintext: {}'.format(payload))