[英]Google Datastore access using Service Account Keys
I have a GCD account within which i created a project and datastore within the latter. 我有一个GCD帐户,我在其中创建了一个项目和数据存储。 I downloaded the service account keys for this project and stored in my local PC.
我下载了该项目的服务帐户密钥,并将其存储在本地PC中。
Now, i have a simple node app running in my PC. 现在,我在PC上运行了一个简单的节点应用程序。 This node app has a simple query to fetch data from google datastore created above.
此节点应用程序具有一个简单的查询,可从上面创建的Google数据存储区中获取数据。 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. oiutput:[数据存储] Dev App Server现在正在运行。
more details: https://cloud.google.com/datastore/docs/tools/datastore-emulator 更多详细信息: https : //cloud.google.com/datastore/docs/tools/datastore-emulator
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.