How to access a remote datastore when running dev_

2019-02-20 01:49发布

I'm attempting to run a localhost web server that has remote api access to a remote datastore using the remote_api_stub method ConfigureRemoteApiForOAuth.

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 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)

1条回答
做自己的国王
2楼-- · 2019-02-20 02:29

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 gcloud for this. Here's how to set it up:

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 authentication tab. Create it, and then download it. Call it client_secret.json or something.

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:

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)

Once that's done, you can make queries like this:

query = client.query(kind='Model').fetch()

It's actually super easy. Any who, that's how I would do that! Cheers.

查看更多
登录 后发表回答