How to access a remote datastore when running dev_appserver.py?

前端 未结 1 1585
忘了有多久
忘了有多久 2021-01-06 07:58

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

1条回答
  •  时光说笑
    2021-01-06 08:49

    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.

    0 讨论(0)
提交回复
热议问题