我有一个运行良好的谷歌云函数,我希望在回调之前执行后连接到 Firestore 以将文档添加到我的 Notifications
集合中。
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore({
projectId: 'my-firebase-project',
keyFilename: 'thekey.json',
});
var fsDocument = {
'username': 'theuser',
'organization': 'theorg',
'attr': [
{k:'key1', v:'val1'},
{k:'key2', v:'val2'}
]
};
firestore.collection('Notifications').add(fsDocument).then(documentReference => {
console.log('Added document with name' + documentReference.id);
});
如何将 key 文件包含到我的谷歌云功能中?到目前为止,我正在 console.cloud.google.com 中创建它们。
最佳答案
部署时,functions
目录中的所有文件都将发送到 Cloud Functions。您可以将您的凭据放在 functions
下的文件中,然后使用这样的相对路径引用它:
const firestore = new Firestore({
projectId: 'my-firebase-project',
keyFilename: './thekey.json',
});
仅当您的凭据用于与运行 Cloud Function 的项目不同的项目时,才应执行此操作。如果您尝试在与运行您的函数的项目相同的项目中访问 Firestore,只需使用 Admin SDK 使用默认凭据即可。在 functions-samples 中有很多这样的例子。
关于node.js - 如何在 Google Cloud Function 中包含 Google Cloud 的 JSON 项目 key 文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47652344/