簡體   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