姜戈 +应用程序引擎 (GAE) - 未检测到本地 .env 或 GOOGLE_CLOUD_PROJECT。没有发现秘密
我想在 App Engine 中部署 Django 应用程序。 我在 GAE 中创建并配置了一个秘密管理器,当我想从 SETTINGS.PY 中获取该秘密时,它显示错误“未检测到本地 .env 或 GOOGLE_CLOUD_PROJECT”。没有发现秘密”。
如果我在本地创建 .env,它可以工作,但我想从 GAE 获取秘密信息。
SETTING.PY
env_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(env_file):
# Use a local secret file, if provided
env.read_env(env_file)
# ...
elif os.environ.get("GOOGLE_CLOUD_PROJECT", None):
# Pull secrets from Secret Manager
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
client = secretmanager.SecretManagerServiceClient()
settings_name = os.environ.get("SETTINGS_NAME", "secret-django-phi")
name = f"projects/{project_id}/secrets/{settings_name}/versions/latest"
payload = client.access_secret_version(name=name).payload.data.decode("UTF-8")
env.read_env(io.StringIO(payload))
else:
raise Exception("No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found.")
REQUIREMENTS.txt
google-cloud-secret-manager==1.0.0
django-environ==0.4.5
秘密管理器,我像 .env 文件一样上传到 GAE
db_ip=x
db_name=x
db_user=x
db_pass=x
SECRET_KEY=*a lot of characters*
i want to deploy in App Engine a Django app.
I created and configurate a SECRET MANAGER in GAE and when i want to get that secret from my SETTINGS.PY, it display the error 'No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found'.
If i create the .env locally it works, but i want to get the secret info from the GAE.
SETTING.PY
env_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(env_file):
# Use a local secret file, if provided
env.read_env(env_file)
# ...
elif os.environ.get("GOOGLE_CLOUD_PROJECT", None):
# Pull secrets from Secret Manager
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
client = secretmanager.SecretManagerServiceClient()
settings_name = os.environ.get("SETTINGS_NAME", "secret-django-phi")
name = f"projects/{project_id}/secrets/{settings_name}/versions/latest"
payload = client.access_secret_version(name=name).payload.data.decode("UTF-8")
env.read_env(io.StringIO(payload))
else:
raise Exception("No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found.")
REQUIREMENTS.txt
google-cloud-secret-manager==1.0.0
django-environ==0.4.5
SECRET MANAGER that i upload on GAE like an .env file
db_ip=x
db_name=x
db_user=x
db_pass=x
SECRET_KEY=*a lot of characters*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在本地运行时,我遇到了同样的错误。事实证明,我在父目录中有.env,但是Google的示例代码假设它在当前目录中。我刚刚改变了:
这
解决了我的问题。
I was getting this same error, while running locally. Turned out I had my .env in the parent directory, but the example code from Google assumes it's in the current directory. I just changed:
to
and that fixed my problem.