Skip to main content
 首页 » 编程设计

python之Python 代码中的 Google Cloud Datastore API

2024年10月17日9zfyouxi

我正在尝试在未在 Google App Engine 上运行的 Python Django 项目中实现 Google Cloud DataStore。

是否可以在项目不在 Google App Engine 上运行的情况下使用 Google Datastore?如果是,您能告诉我如何检索完整的实体对象或成功执行查询吗?

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

代码片段:

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可能会有帮助。你可以看到它的一个例子here .

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

datastore.Client("YOUR_PROJECT_ID") 

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

$ gcloud beta auth application-default login 

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

希望这对您有所帮助!