Connecting to Google Datastore using gcloud

575 views Asked by At

I am trying to connect to my Google Datastore instance but I can't find any sample code for how to initialize a connection!

Gcloud's main documentation immediately starts with how to assign an Entity or a Query but skips the crucial connection bit. The demo section seems specific to the demo in that it uses test environment variables which is quite confusing.

Most importantly, I am interested in what environment variables I need to define and if it is possible to define the service email and path to key from within the python program.

Highly appreciate it if anyone can provide a sample code of how to commit something to a datastore instance.

Thanks!

2

There are 2 answers

0
Moataz Elmasry On BEST ANSWER

After looking around, I finally found the environment variables I need define.

To connect to your google datastore from within Python:

import os

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = <path to private key>
os.environ['GCLOUD_DATASET_ID'] = <dataset_id, also known as project id>

# Fetching queries should work now
query = datastore.Query(kind=<kind>, namespace=<namespace>)
for result in query.fetch():
    print result

Google assumes you are using App Engine with Datastore that is why it is trickier to find these variables if you first introduction to Google Cloud is its Datastore service.

1
Bill Prin On

Creating a Service Account in the Developer's Console Credential section and downloading the JSON key, then having the GOOGLE_APPLICATION_CREDENTIALS environment variable point to it is one approach if you are not using Appengine or Compute engine.

You normally wouldn't do that in code like you did since it's specific to a given environment.

If you are using Compute Engine, you can just create the instance with the Datastore scope and then that step is unnecessary:

gcloud compute instances create $INSTANCE_NAME --scopes datastore userinfo-email

Also, another way to specify the project id is like this:

from gcloud import datastore
datastore.set_defaults(dataset_id='project-id')