我想在 localhost 上将新的数据存储区模拟器与GAE Flask应用程序一起使用。我想在Docker环境中运行它,但是无论是否使用Docker,我都会遇到(DefaultCredentialsError
)错误。
我的Flask文件如下所示(see the whole repository here on GitHub):
main.py:
from flask import Flask
from google.cloud import datastore
app = Flask(__name__)
@app.route("/")
def index():
return "App Engine with Python 3"
@app.route("/message")
def message():
# auth
db = datastore.Client()
# add object to db
entity = datastore.Entity(key=db.key("Message"))
message = {"message": "hello world"}
entity.update(message)
db.put(entity)
# query from db
obj = db.get(key=db.key("Message", entity.id))
return "Message for you: {}".format(obj["message"])
index()
处理程序工作正常,但是message()
处理程序抛出此错误:
[2019-02-03 20:00:46,246] ERROR in app: Exception on /message [GET]
Traceback (most recent call last):
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/main.py", line 16, in message
db = datastore.Client()
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/google/cloud/datastore/client.py", line 210, in __init__
project=project, credentials=credentials, _http=_http
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/google/cloud/client.py", line 223, in __init__
_ClientProjectMixin.__init__(self, project=project)
INFO 2019-02-03 20:00:46,260 module.py:861] default: "GET /message HTTP/1.1" 500 291
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/google/cloud/client.py", line 175, in __init__
project = self._determine_default(project)
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/google/cloud/datastore/client.py", line 228, in _determine_default
return _determine_default_project(project)
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/google/cloud/datastore/client.py", line 75, in _determine_default_project
project = _base_default_project(project=project)
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/google/cloud/_helpers.py", line 186, in _determine_default_project
_, project = google.auth.default()
File "/tmp/tmpJcIw2U/lib/python3.5/site-packages/google/auth/_default.py", line 306, in default
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
我在错误日志中检查了网站并尝试了JSON身份验证文件(GOOGLE_APPLICATION_CREDENTIALS
),但结果是我的应用程序随后连接了Google Cloud上的生产数据存储区本地数据存储模拟器。
有什么办法解决这个问题吗?
答案 0 :(得分:0)
需要凭据的事实表明您正在访问实际的数据存储,而不是数据存储模拟器(不需要或不请求凭据)。
要到达仿真器,客户端应用程序(支持它)需要弄清楚仿真器正在侦听的位置,为此,您需要为其设置DATASTORE_EMULATOR_HOST
环境变量。来自Setting environment variables:
启动仿真器后,需要设置环境变量,以便 您的应用程序连接到仿真器而不是 生产数据存储模式环境。设置这些环境变量 在用于运行应用程序的同一台计算机上。
每次启动时都需要设置环境变量 仿真器。环境变量取决于动态分配 重新启动仿真器时可能会更改的端口号。
有关设置环境的详细信息,请参阅本节的其余部分,并可能浏览Is it possible to start two dev_appserver.py connecting to the same google cloud datastore emulator?
答案 1 :(得分:0)
我设法通过将env vars直接添加到Python代码(在本例中为main.py)并使用Mock库来解决此问题:
import os
import mock
from flask import Flask, render_template, request
from google.cloud import datastore
import google.auth.credentials
app = Flask(__name__)
if os.getenv('GAE_ENV', '').startswith('standard'):
# production
db = datastore.Client()
else:
# localhost
os.environ["DATASTORE_DATASET"] = "test"
os.environ["DATASTORE_EMULATOR_HOST"] = "localhost:8001"
os.environ["DATASTORE_EMULATOR_HOST_PATH"] = "localhost:8001/datastore"
os.environ["DATASTORE_HOST"] = "http://localhost:8001"
os.environ["DATASTORE_PROJECT_ID"] = "test"
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
db = datastore.Client(project="test", credentials=credentials)
然后像这样运行数据存储模拟器:
gcloud beta emulators datastore start --no-legacy --data-dir=. --project test --host-port "localhost:8001"
所需需求:
Flask
google-cloud-datastore
mock
google-auth
此处的GitHub示例:https://github.com/smartninja/gae-2nd-gen-examples/tree/master/simple-app-datastore