【问题标题】:How to include the Google Cloud's JSON Project Key file in a Google Cloud Function?如何在 Google Cloud 函数中包含 Google Cloud 的 JSON 项目密钥文件?
【发布时间】:2018-05-19 00:56:02
【问题描述】:

我有一个运行良好的 Google Cloud 函数,我希望在回调之前执行一次后,连接到 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);
});

如何将密钥文件包含到我的谷歌云功能中?到目前为止,我正在 console.cloud.google.com 中创建它们。

【问题讨论】:

    标签: node.js firebase google-cloud-platform google-cloud-functions google-cloud-firestore


    【解决方案1】:

    在您部署时,functions 目录中的所有文件都将发送到 Cloud Functions。您可以将您的凭据放在functions 下的文件中,然后使用这样的相对路径引用它:

    const firestore = new Firestore({
      projectId: 'my-firebase-project',
      keyFilename: './thekey.json',
    });
    

    仅当您的凭据用于与运行 Cloud Function 的项目不同的项目时,您才应该这样做。如果您尝试在与运行您的函数的项目相同的项目中访问 Firestore,只需使用 Admin SDK 使用默认凭据即可。 functions-samples中有很多这样的例子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 2016-04-08
      • 2018-02-14
      • 1970-01-01
      • 2019-02-11
      相关资源
      最近更新 更多