Google 使用服务帐户密钥访问数据存储区

Google Datastore access using Service Account Keys

我有一个 GCD 帐户,我在其中创建了一个项目和数据存储。我下载了该项目的服务帐户密钥并存储在我的本地 PC 中。

现在,我的 PC 中有一个简单的节点应用程序 运行。此节点应用程序有一个简单的查询,可从上面创建的 google 数据存储中获取数据。我需要知道的是我应该如何在我的节点应用程序中配置服务帐户密钥以访问数据存储和相应的实体,因为当我尝试访问时出现这样的错误 - "Missing or insufficient permissions."

节点应用查询-

// 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);

您必须创建数据存储模拟器。

请按照以下步骤操作。

gcloud components install cloud-datastore-emulator

gcloud beta emulators datastore start

oiutput:[datastore] Dev App Server 现在是 运行。

更多详情:https://cloud.google.com/datastore/docs/tools/datastore-emulator

要使用服务帐户凭据,请将您的客户端代码更改为:

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