【问题标题】:Google Datastore access using Service Account Keys使用服务帐户密钥访问 Google 数据存储区
【发布时间】:2019-06-12 07:40:35
【问题描述】:

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

现在,我的 PC 上运行了一个简单的节点应用程序。这个节点应用程序有一个简单的查询来从上面创建的谷歌数据存储中获取数据。我只需要知道我应该如何在我的节点应用程序中配置服务帐户密钥以获取对数据存储区和相应实体的访问权限,因为当我尝试访问时出现这样的错误 - “权限缺失或不足。”

节点应用查询 -

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

【问题讨论】:

    标签: node.js authentication google-cloud-platform google-cloud-datastore


    【解决方案1】:

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

    请按照以下步骤操作。

    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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我可以尝试这个选项,但之前我错误地设置了“GOOGLE_APPLICATION_CREDENTIALS”路径变量。我的节点应用程序抛出错误 - “错误:“C:\Masters-LUC\spring-2019\internship\service-keys\xxx.json”中的文件不存在,或者它不是文件。错误:ENOENT:没有这样的文件或目录,lstat 'C:\Masters-LUC\spring-2019\internship\mean\mean-course\"C:' at GoogleAuth.createError (C:\Masters-LUC\spring-2019\internship\mean \mean-course\node_modules\google-auth-library\build\src\auth\googleauth.js:426:16)" 。我应该怎么做才能解决这个错误??
      • 指定正确的路径。我会创建一个更短的目录,例如:c:\google 并将您的凭据文件放在那里。
      • 在另一个 stackoverflow 链接上出现了类似的错误 - stackoverflow.com/questions/51998564/… 所以我删除并重新安装了 npm 包 - google-auth-library。后来我使用您的建议/解决方案来运行我的节点应用程序。有效。非常感谢约翰
      猜你喜欢
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多