2

I have my nodejs directory which looks something like this:

MyApp
    -app.js
    -mycredetnials.json
    -package.json
    +node_modules

Inside of my app.js file I have the following code:;

var options = {
    projectId: 'mydatastoreproject'
}

var keyFile = './mydatastoreprojectsandbox-5265e25422c.json';

if (keyFile) {
  options.keyFilename = keyFile;
}

var datastore = gcloud.datastore(options);

I am running this from my local computer. I want my local computer to be able to connect to the Google Cloud Datastore (not the local emulator).

In my developer console I have enabled Google Datastore API. I created a KIND in the developer console and created one entity. I have also created credentials (service account key) in the form of a json file. I then included this json file in my code. By doing this I am still unable to connect to the datastore. Am I missing something or doing something wrong? Essentially all I want to do is to have my local machine connect to the Google Datastore.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
user2924127
  • 6,034
  • 16
  • 78
  • 136
  • There is a remote api for Python to access the datastore from your local machine. I am not sure anything exists for nodejs https://cloud.google.com/appengine/docs/python/tools/remoteapi – gipsy May 18 '16 at 10:52
  • how to connect to gcp datastore/bigquery from nodejs pcf environment – Vinod Kumar Marupu Aug 16 '17 at 11:40

3 Answers3

2

When you start the local datastore and it prints out "Set these environment variables", did you do that? Is that how you've been connecting to your local Datastore so far? By default, gcloud-node tries to talk to the remote API and assumes there is no emulator running. It has to be told to talk to the emulator, which can be done via the env var (DATASTORE_EMULATOR_HOST). If that is what you've been setting, you will have to unset it (or manually do it from the JS script: delete process.env.DATASTORE_EMULATOR_HOST;).

(Full answer: https://github.com/GoogleCloudPlatform/gcloud-node/issues/1328)

Stephen
  • 5,710
  • 1
  • 24
  • 32
  • This should be the selected answer. This was helpful; the fact that my local Env vars were overriding that value was not immediately obvious. – User 1058612 Jun 17 '21 at 19:34
1

I have not tried for ptyhon but I think this can work for you too. I've tried with go and using this library, you should install gcloud and login from console with gcloud beta auth application-default login,this solves tons of headache, I found it accidentally reading this article

John Balvin Arias
  • 2,632
  • 3
  • 26
  • 41
1

Sounds like you already have exactly what you need assuming mydatastoreprojectsandbox-5265e25422c.json is your google service api key.

If not you can grab a Google Service API Key from this link: https://console.cloud.google.com/apis/credentials/serviceaccountkey?_ga=fuckuranalytics

The Google Service API key .json file is what enables your localhost node/npm project to connect with real remote google datastore instance. The node library for accessing the datastore automatically checks an environment variable on your system containing a path to this key file. If set properly, your local node/npm project will be automatically connected to the remote google datastore your key file has access to.

The environment variable can be set via:

Mac/Linux (via command line)

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"

Windows (via powershell)

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"

More information about google's environment variable auth for datastore & node/npm: https://cloud.google.com/docs/authentication/getting-started#linux-or-macos

Llama D'Attore
  • 323
  • 1
  • 12