运行dev_appserver.py时如何访问远程数据存储? [英] How to access a remote datastore when running dev_appserver.py?
问题描述
我尝试使用 我一直在使用下面的Google文档作为参考,但发现它相当稀少: https://cloud.google.com/appengine/docs/python/tools/remoteapi 我相信我缺少认证位,但找不到具体的资源来指导我。给出以下代码示例,在运行 这个get问了很多,只是因为你不能在SDK之外使用app引擎库。但是,在App Engine SDK中也有一种更简单的方法。 我会使用 如果您想在App Engine环境内部或外部与Google云存储服务交互,可以使用Gcloud( https://googlecloudplatform.github.io/gcloud-python/stable/ )。 / p> 您需要在您的应用程序上使用服务帐户,并下载JSON凭证文件。您可以在 使用这些,一旦你用pip安装了适用于gcloud的软件包,您可以进行查询以及写入数据。 以下是使用该库进行身份验证的示例: 完成后,您可以进行如下查询: 这其实很容易。任何人,我就是这么做的!干杯。 I'm attempting to run a localhost web server that has remote api access to a remote datastore using the I have been using the following Google doc for reference but find it rather sparse: https://cloud.google.com/appengine/docs/python/tools/remoteapi I believe I'm missing the authentication bit, but can't find a concrete resource to guide me. What would be the easiest way, given the follow code example, to access a remote datastore while running
This get's asked a lot, simply because you can't use app engines libraries outside of the SDK. However, there is also an easier way to do it from within the App Engine SDK as well. I would use If you want to interact with google cloud storage services inside or outside of the App Engine environment, you may use Gcloud (https://googlecloudplatform.github.io/gcloud-python/stable/) to do so. You need a service account on your application as well as download the JSON credentials file. You do this on the app engine console under the With those, once you install the proper packages for gcloud with pip, you'll be able to make queries as well as write data. Here is an example of authenticating yourself to use the library: Once that's done, you can make queries like this: It's actually super easy. Any who, that's how I would do that! Cheers. 这篇关于运行dev_appserver.py时如何访问远程数据存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! remote_api_stub
方法 ConfigureRemoteApiForOAuth $ c来运行远程api访问远程数据存储的本地主机web服务器
dev_appserver.py
?
时,访问远程数据存储最简单的方法是什么? pre> 从google.appengine.ext导入webapp2
从google.appengine.ext.remote_api导入ndb
导入remote_api_stub
class Topic(ndb.Model):
created_by = ndb.StringProperty()
subject = ndb.StringProperty()
@classmethod
def query_by_creator(cls,创建者):
return cls.query(Topic.created_by == creator)
$ b class MainPage(webapp2.RequestHandler):
def get(self):
remote_api_stub.ConfigureRemoteApiForOAuth(
'#####。appspot.com',
'/ _ah / remote_api'
)
topics = Topic.query_by_creator('bill ')
self.response.headers ['Content-Type'] ='text / plain'
self.response.out.write('< html>< body>')
自我(10):
self.response.out.write('< h1>主题主题:< h1>)
。 h3>'+ topic.subject +'< h3>)
self.response.out.write('< / body>< / html>)
app = webapp2.WSGIApplication([
('/',MainPage)
],debug = True)
gcloud
为此。以下是设置方法:
身份验证
标签下的应用引擎控制台上执行此操作。创建它,然后下载它。把它称为 client_secret.json
或其他东西。
from gcloud import datastore
#本地计算机上JSON文件的位置
os.environ [GOOGLE_APPLICATION_CREDENTIALS] =/location/client_secret.json
#开发者控制台中的项目ID
projectID =THE_ID_OF_YOUR_PROJECT
os.environ [ GCLOUD_TESTS_PROJECT_ID] = projectID
os.environ [GCLOUD_TESTS_DATASET_ID] = projectID
client = datastore.Client(dataset_id = projectID)
query = client .query(kind ='Model')。fetch()
remote_api_stub
method ConfigureRemoteApiForOAuth
.dev_appserver.py
?import webapp2
from google.appengine.ext import ndb
from google.appengine.ext.remote_api import remote_api_stub
class Topic(ndb.Model):
created_by = ndb.StringProperty()
subject = ndb.StringProperty()
@classmethod
def query_by_creator(cls, creator):
return cls.query(Topic.created_by == creator)
class MainPage(webapp2.RequestHandler):
def get(self):
remote_api_stub.ConfigureRemoteApiForOAuth(
'#####.appspot.com',
'/_ah/remote_api'
)
topics = Topic.query_by_creator('bill')
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('<html><body>')
self.response.out.write('<h1>TOPIC SUBJECTS:<h1>')
for topic in topics.fetch(10):
self.response.out.write('<h3>' + topic.subject + '<h3>')
self.response.out.write('</body></html>')
app = webapp2.WSGIApplication([
('/', MainPage)
], debug=True)
gcloud
for this. Here's how to set it up:authentication
tab. Create it, and then download it. Call it client_secret.json
or something.from gcloud import datastore
# the location of the JSON file on your local machine
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/location/client_secret.json"
# project ID from the Developers Console
projectID = "THE_ID_OF_YOUR_PROJECT"
os.environ["GCLOUD_TESTS_PROJECT_ID"] = projectID
os.environ["GCLOUD_TESTS_DATASET_ID"] = projectID
client = datastore.Client(dataset_id=projectID)
query = client.query(kind='Model').fetch()