簡體   English   中英

使用服務帳戶密鑰訪問Google數據存儲區

[英]Google Datastore access using Service Account Keys

我有一個GCD帳戶,我在其中創建了一個項目和數據存儲。 我下載了該項目的服務帳戶密鑰,並將其存儲在本地PC中。

現在,我在PC上運行了一個簡單的節點應用程序。 此節點應用程序具有一個簡單的查詢,可從上面創建的Google數據存儲區中獲取數據。 我所需要知道的是,我應該如何在節點應用程序中配置服務帳戶密鑰以訪問數據存儲區和相應的實體,因為在嘗試訪問時出現這樣的錯誤-“缺少權限或權限不足”。

節點應用查詢-

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

要使用服務帳戶憑據,請將您的客戶端代碼更改為:

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

您必須創建一個數據存儲模擬器。

請按照下面的步驟。

gcloud components install cloud-datastore-emulator

gcloud beta emulators datastore start

oiutput:[數據存儲] Dev App Server現在正在運行。

更多詳細信息: https : //cloud.google.com/datastore/docs/tools/datastore-emulator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM