如何从我的Compute Engine VM访问我的AppEngine DataStore实体?

时间:2014-09-01 08:53:00

标签: google-app-engine google-compute-engine google-cloud-datastore app-engine-ndb

我的应用程序在App Engine上运行,但我想从我的Compute Engine VM访问其NDB DataStore实体来进行一些处理并将结果写回App Engine DataStore。我怎样才能做到这一点?

此外,Google Cloud DataStore和App Engine DataStore是一样的吗? https://developers.google.com/datastore/ https://developers.google.com/appengine/docs/python/ndb/

4 个答案:

答案 0 :(得分:9)

David的解决方案要求您使用App Engine实例时间发出请求,但您可以绕过它并直接从Compute Engine实例向Datastore发出请求。关于如何做到这一点非常好tutorial。但它不像ndb那么漂亮。

>>> import googledatastore as datastore
>>> datastore.set_options(dataset='project-id')
>>> req = datastore.BeginTransactionRequest()
>>> datastore.begin_transaction(req)
<datastore.datastore_v1_pb2.BeginTransactionResponse object at ...>

答案 1 :(得分:3)

Google Cloud Datastore是App Engine数据存储的独立版本。

回到你的问题,你有两个选择:

  1. 编写Web服务,将您的实体从App Engine应用程序公开给计算引擎VM。一个选项是Cloud Endpoints。 Cloud Endpoints使用OAuth2身份验证和Compute Engine VMs are shipped with OAuth2 service accounts that you can use to authenticate to the service

  2. 使用App Engine remote API访问数据存储区。远程API提供对数据存储的访问,就像您在App Engine实例中一样。但默认情况下,API要求您提供App Engine amdin的密码,因此您可能必须将密码存储在计算引擎VM中,这是不安全的。

答案 2 :(得分:2)

除了@David解释的选项之外,您还可以查看Managed VMs:它们是Compute Engine和App Engine之间的交叉 - 基本上是一个可以访问App Engine服务的托管Compute Engine实例。

答案 3 :(得分:1)

官方推荐的方法是使用数据存储客户端库;见https://cloud.google.com/datastore/docs/reference/libraries 您需要创建服务帐户,或使用标准计算引擎服务帐户,授予所有API或该服务帐户的数据存储区的权限,并创建计算引擎实例以成为该服务帐户的一部分。有关详细信息,请参阅here。 然后你可以做类似的事情:

from google.auth import compute_engine                                                                                      
from google.cloud import datastore                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

datastore_client = datastore.Client(project='yourproject',     
                                    credentials=compute_engine.Credentials())                                                                                                                                                                  
q = datastore_client.query(kind='YourEntity')                                                                                    
q.add_filter('field_name', '=', 'HelloThere') 
print list(q.fetch(1))