简体   繁体   中英

Google Datastore access using Service Account Keys

I have a GCD account within which i created a project and datastore within the latter. I downloaded the service account keys for this project and stored in my local PC.

Now, i have a simple node app running in my PC. This node app has a simple query to fetch data from google datastore created above. All i need to know is how should i configure the service account keys in my node app to gain access to datastore and the corresponding entities because i get an error like this when i try to access - "Missing or insufficient permissions."

Node app query -

// Adding a Sample Entity
async function quickStart() {
  // Your Google Cloud Platform project ID
  const projectId = 'XXX';

  // Creates a client
  const datastore = new Datastore({
    projectId: projectId,
  });

  // The kind for the new entity
  const kind = 'xxx';
  // The name/ID for the new entity
  const name = xxxx;
  // The Cloud Datastore key for the new entity
  const sampleKey = datastore.key([kind, name]);

  const [entity] = await datastore.get(sampleKey);
  console.log(entity);
  }
quickStart().catch(console.error);

To use Service Account credentials change your client code to:

// Creates a client
const datastore = new Datastore({
    projectId: projectId,
    keyFilename: '/path/to/keyfile.json'
});

You must create a Datastore Emulator.

Please follow the steps bellow.

gcloud components install cloud-datastore-emulator

gcloud beta emulators datastore start

oiutput: [datastore] Dev App Server is now running.

more details: https://cloud.google.com/datastore/docs/tools/datastore-emulator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM