Per Google's Cloud Datastore Emulator installation instructions, I was able to install and run the emulator in a bash terminal window without problem with gcloud beta emulators datastore start --project gramm-id.
I also setup the environment variables, per the instructions, in another terminal with $(gcloud beta emulators datastore env-init) and verified they were defined.
However, when I run my python script to add an entity to the local datastore with this code:
from google.cloud import datastore
print(os.environ['DATASTORE_HOST']) # output: http://localhost:8081
print(os.environ['DATASTORE_EMULATOR_HOST']) # output: localhost:8081
client = datastore.Client('gramm-id')
kind = 'Task'
name = 'simpleTask'
task_key = client.key(kind, name)
task = client.Enity(key=task_key)
task['description'] = 'Buy milk'
client.put(task)
I get the error:
Traceback (most recent call last):
File "tools.py", line 237, in <module>
client = datastore.Client('gramm-id')
File "/home/.../lib/python3.6/site-packages/google/cloud/datastore/client.py", line 205, in __init__
project=project, credentials=credentials, _http=_http)
... long stack trace ....
File "/home/.../lib/python3.6/site-packages/google/auth/_default.py", line 306, in default
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://developers.google.com/accounts/docs/application-default-credentials.
I don't think I need to create a GCP service account and provide access credentials to use the datastore emulator on my machine.
My system:
What am I missing?
gcloud auth application-default login
This will prompt you to login through a browser window and will set your GOOGLE_APPLICATION_CREDENTIALS correctly for you. [1]
In theory you should be able to use mock credentials, e.g.:
class EmulatorCreds(google.auth.credentials.Credentials):
def __init__(self):
self.token = b'secret'
self.expiry = None
@property
def valid(self):
return True
def refresh(self, _):
raise RuntimeError('Should never be refreshed.')
client = datastore.Client(
project='gramm-id',
credentials=EmulatorCreds() ,
_http=requests.Session() # Un-authorized
)
However it seems like this doesn't currently work, so for now you'll need to set GOOGLE_APPLICATION_CREDENTIALS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With