Reputation: 1577
I am trying to test google's cloud datastore locally. My test steps are:
I followed all the steps in order to use the local emulator
gcloud beta emulators datastore start
gcloud beta emulators datastore env-init
However, in python, when use the following command to access cloud datastore, it always save the data directly to google cloud instead of saving them to the local emulators
#Imports the Google Cloud client library
from google.cloud import datastore
# Instantiates a client
datastore_client = datastore.Client()
sample_entry = some_data
# Saves the entity
datastore_client.put(sample_entry)
It seems like you cannot specify the library to use the local datastore emulator, just like what they offer in their Node.js client
var datastore = gcloud.datastore({
apiEndpoint: "http://localhost:8380"
});
My question is, How can I ask the google cloud datastore python library to use local emulator instead of using the cloud directly
Upvotes: 9
Views: 1781
Reputation: 3217
You can try something like
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
# Production
else:
# Local development server
You can follow more here, https://cloud.google.com/appengine/docs/standard/python/tools/using-local-server
Upvotes: 0
Reputation: 542
You need to eval $(gcloud beta emulators datastore env-init)
.
gcloud beta emulators datastore env-init
only prints the commands that set the necessary environment variables.
Upvotes: 1