我正在尝试将应用程序部署到谷歌云,但收到环境变量密钥错误。
这是一个烧瓶应用程序,顶部的
main.py
为
import flask from Flask, render_template, request
from dotenv import load_dotenv
import os, stripe, ...
load_dotenv()
stripe.api_key = os.environ['stripe_key']
...
我的应用程序在本地运行。我使用
python main.py
运行它,没有收到任何错误,并且在提交 Web 表单后,数据传播到 stripe。
当我将其部署到谷歌云时,使用
gcloud app deploy
,我收到了
ERROR: (gcloud.app.deploy) Error Response: [9] An internal error occured while processing task /app-engine-flex/flex_await_healthy/flex_await_healthy>2024 ... Starting gunicorn 20.1.0
...
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
...
stripe.api_key = os.environ['stripe_key']
^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen_os>", line 685, in __getitem__
KeyError: 'stripe_key'
我的
stripe_key
文件中有 .env
作为
...
stripe_key='sk_test_*********'
我在谷歌云中设置了环境变量,遵循this,我的项目>云函数>我的函数>变量并将其设置为
stripe_key
sk_test_****
不带单引号。
在云功能下有信息
Environment | Name | Region | Trigger | Runtime | Executed function |
---------------------------------------------------------------------------------------
2nd gen function-1 us-central1 HTTP Python 3.12 hello_http
我正在自己的系统上运行
Python 3.12.0
。
我尝试再次部署它,但收到了不同的错误:
ERROR: (gcloud.app.deploy) Error Response: [4] An internal error occured while processing task /app-engine-flex/flex_await_healthy/flex_await_healthy>2024... Timed out waiting for the flex deployment to become network provisioned.
我的
app.yaml
是
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
operating_system: ubuntu22
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
.gitignore
是
venv/
.env
templates/auth/node_modules
.gcloudignore
是
.env
/venv/
templates/auth/node_modules
.flaskenv
是
FLASK_APP=main.py
FLASK_ENV=development
requirements.txt
是
Flask==3.0.0; python_version > '3.6'
Flask==2.3.3; python_version < '3.7'
Werkzeug==3.0.1; python_version > '3.6'
Werkzeug==2.3.7; python_version < '3.7'
gunicorn==20.1.0
python-dotenv==1.0.1
stripe==7.13.0
...
目录结构
myproject
|
|--static
|--templates
| |--my_templates
| |--index.html
|
|--.env
|--.flaskenv
|--.gitignore
|--app.yaml
|--main.py
|--requirements.txt
您已将
.env
文件包含在 .gcloudignore
配置文件中。这将导致在部署应用程序时删除该文件。
如果您的应用程序依赖它,请尝试从该文件中删除
.env
条目。
参考:https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore