Unable to call datastore using GCP service account key json

28 views Asked by At

I am using nestJS (typescript) for developing server. I have to access GCP datastore to update and fetch data.

Currently I am doing development locally so I want to access GCP datastore locally.

In GCP console, I have done following steps in already created project:

  1. Created Service Account with role as Owner.
  2. I went to keys section of Service account and created new key and downloaded json.

In my nestJS project, I have installed to npm package "@google-cloud/datastore" to access datastore through library.

I have set .env file value:

GOOGLE_APPLICATION_CREDENTIALS=gcp-key.json

I have datastoreService.ts file to perform CRUD action on datastore


@Injectable()
@Injectable({ scope: Scope.TRANSIENT }) 
export class DatastoreService {
  datastore: Datastore;
  kind: string;

  constructor(
    @InjectPinoLogger(DatastoreService.name) private logger: PinoLogger,
  ) {
    this.datastore = new Datastore();
  }

  async upsert(data: any, id: string, kindName = this.kind): Promise<any> {
    try {
      this.logger.info(`Upsert operation in Datastore for id : ${JSON.stringify(id)}`);
      let taskKey = this.datastore.key([kindName, id]);
      let task = {
        key: taskKey,
        data: data
      };
      this.logger.info(`task: ${task}`);
      return this.datastore.upsert(task);
    }
    catch (err) {
      this.logger.error(`Error querying Datastore ${JSON.stringify(err)}`);
      throw err;
    }
  }

}

Getting error:

 {"code":5,"details":"","metadata":{"x-debug-tracking-id":["xxxxxxxxx;o=1"]}}

After checking online , Code 5 means auth related issue.

Things I tried:

  1. I had provided Service Account permission and IAM role with owner .
  2. Later I added all Datastore related role also.
  3. Later , In Datastore client of DatastoreService, I passed projectId , keyFilename (absolute path), namespace.

But I am still facing same issue. Please point out where I am doing wrong.

Please point out where I am doing wrong.

0

There are 0 answers