繁体   English   中英

谷歌云存储的 firebase 云函数 API 错误

[英]firebase cloud function API Error with Google Cloud Storage

随着 Firebase Cloud Functions 的引入,我们正在考虑将我们当前的一些 node.js 服务器端代码移动到云函数中。 我遇到的一个问题是将文件从 GCS 存储桶下载到磁盘上的临时文件,然后将其作为附件通过电子邮件发送(使用 mailgun-js)。

让我伤心的一段代码是:

return mkdirp(tempLocalDir).then(() => {
    const bucket = gcs.bucket(gcsBucket);
    const tempFilePath = tempLocalDir + gcsFile;
    return bucket.file(gcsFile).download({
        destination: tempFilePath
    }).then(() => {
        console.log('File downloaded locally to', tempFilePath);
        var messageSubject = "Test";
        var messageBody = "Test with attach";

        var mailgunData = {
            from: ,
            to: agentEmail,
            subject: messageSubject,
            html: messageBody,
            attachment: tempFilePath,
        };
        mailgunAgent.messages().send(mailgunData, function (error, body) {
            console.log(body);
        });

    });

});

我在函数日志中收到的错误消息是:

ApiError: Forbidden
    at Object.parseHttpRespMessage (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:156:33)
    at Object.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:131:18)
    at Duplexify.<anonymous> (/user_code/node_modules/@google-cloud/storage/src/file.js:724:21)
    at emitOne (events.js:96:13)
    at Duplexify.emit (events.js:188:7)
    at emitOne (events.js:96:13)
    at DestroyableTransform.emit (events.js:188:7)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Request.<anonymous> (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1108:14)

我可以使用 request 将文件下载到磁盘上的 /tmp/ 文件夹,这将是后备选项,但如果可能的话,我真的很想使用 GCS 工具。 我“认为”这是 GCS 的身份验证错误,但我不确定如何追踪。 对于 GCS 和 Firebase,我是否需要在云函数 .config() 中使用不同的身份验证参数? 如果是这样,我如何输入它们? 我们的 GCS 存储桶和项目早于 Firebase Storage 的推出,但我们已经成功地将它与在我们服务器上运行的节点功能一起使用。

提前致谢,扎克

已解决:终于解决了上述问题。 问题是 @google-cloud/storage auth 需要 GCS 项目 ID 作为 ID,但需要 Firebase admin-sdk 密钥文件,而不是 GCS 项目密钥文件。 使用 Firebase 项目 ID 作为带有 Firebase 密钥文件的 GCS 身份验证中的 ID 也失败了。

¯_(ツ)_/¯

仅在我们的项目设置中可能是一个奇怪的行为,但认为它与为我们解决问题的更新相关。

使用上面grll提供的格式工作的配置是:

const gcs = require('@google-cloud/storage');
const storageClient = gcs({
    projectId: "this is the GCS project ID,
    keyFilename: 'this is the firebase admin-sdk keyFilename.json'
});
const bucket = storageClient.bucket(gcsBucket);

无需附加 projectId 或 keyFilename 即可解决此问题的另一种方法:

  1. 转到您项目的 Firebase 控制台
  2. 设置 -> 权限
  3. 应打开 IAM 选项卡
  4. 现在为“存储管理员”角色授予您的“App Engine 默认服务帐户”和“Google API 服务帐户”权限
  5. 现在应该没问题了!

您需要先进行识别,然后才能使用谷歌云存储 API。 或者任何其他 API。

一种方法是转到您在谷歌云平台上的 API 登录页面:在这里下载相应的服务帐户密钥作为 JSON 文件。 然后将此文件放在 firebase 项目内的函数文件夹中。

最后而不是使用:
const bucket = gcs.bucket(gcsBucket);

你会称之为:

const gcs = require('@google-cloud/storage');

const storageClient = gcs({ projectId: projectId, keyFilename: './keyFilename.json' }); const bucket = storageClient.bucket(gcsBucket);
使用您的 projectId 和 keyFilename。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM