Google 云数据存储 API Python 代码

Google Cloud Datastore API in Python Code

我正在尝试在我的 Python Django 项目中实施 Google Cloud DataStore,而不是 运行 在 Google App Engine 上实施。

是否可以在 Google App Engine 上没有项目 运行 的情况下使用 Google 数据存储?如果是,能否请您告知如何检索完整的实体对象或成功执行查询?

下面的代码片段打印了查询对象,但之后抛出错误。

代码片段:

from gcloud import datastore
entity_kind = 'EntityKind'
numeric_id = 1234

client = datastore.Client()
key = client.key(entity_kind, numeric_id)
query = client.query(kind=entity_kind)
print(query)
results = list(query.fetch())
print(results)

错误:

NotFound: 404 The project gproj does not exist or it does not contain an active App Engine application. Please visit http://console.developers.google.com to create a project or https://console.developers.google.com/appengine?project=gproj to add an App Engine application. Note that the app must not be disabled.

这个guide will probably be helpful. You can see an example of it in action here.

您只需将项目 ID 传递给 .Client() 方法:

datastore.Client("YOUR_PROJECT_ID")

您还可以通过在 运行 应用程序之前 运行 执行此命令来跳过此部分:

$ gcloud beta auth application-default login

如果您 运行 这样做,它将在本地验证您的所有请求,而无需注入项目 ID :)

希望对您有所帮助!