firebase - ApiError : Error during request while uploading file from Firebase Function

标签 firebase google-cloud-functions firebase-storage

我正在解析一些从 MailGun 转发的电子邮件,在尝试存储附件时出现以下错误:

ApiError: Error during request.
        at Object.parseHttpRespBody (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:193:32)
        at Object.handleResp (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:137:18)
        at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:496:12
        at Request.onResponse [as _callback] (/user_code/node_modules/firebase-admin/node_modules/retry-request/index.js:195:7)
        at Request.self.callback (/user_code/node_modules/firebase-admin/node_modules/request/request.js:186:22)
        at emitTwo (events.js:106:13)
        at Request.emit (events.js:191:7)
        at Request.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/request/request.js:1163:10)
        at emitOne (events.js:96:13)
        at Request.emit (events.js:188:7)

这是初始化
// Initializes the database and storage
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const storage = admin.storage();
const bucket = storage.bucket('gs://my-bucket-address');

我正在通过 busboy 获取文件
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
    // Note that os.tmpdir() is an in-memory file system
    const filepath = path.join(tmpdir, filename)
    uploads[fieldname] = filepath;
    file.pipe(fs.createWriteStream(filepath));
});

这是失败的地方
busboy.on('finish', () => {
    const body_text = formData['body-plain'];
    const body_data = JSON.parse(body_text);
    const sender = body_data.sender;

    for (const name in uploads) {
        const file = uploads[name];

        const metadata = { contentType: 'image/jpeg'};
        const random_id = uuid4();
        const destination = `images/${sender}/${random_id}.jpg`;

        bucket.upload(filepath, { destination: destination, metadata: metadata });
        fs.unlinkSync(file);
    }

});

最佳答案

我不得不使用 GCS 而不是 functions.storage.bucket。我也遇到了与 GCS 相同的问题,但那是因为我在使用 initializeApp 初始化应用程序之前初始化了 GCS .这导致 GCS 未正确验证。我只是哪个 API 错误更有帮助。

编辑:添加代码以供引用

// Import the modules
const functions = require('firebase-functions');
const admin = require('firebase-admin');

// Initialize Admin FIRST
admin.initializeApp();

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

// Than you can access the bucket
const bucket = gcs.bucket('your-bucket.appspot.com');

您可以找到您的存储桶网址 here

关于firebase - ApiError : Error during request while uploading file from Firebase Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48739965/

相关文章:

java - 单击 RecyclerView 项目时出现 NullPointerException

javascript - 获取文档时出错 : clientDetails Error: Cannot modify a WriteBatch that has been committed

google-cloud-platform - 移动数据时,Dataflow 相对于云功能有何优势?

android - 如何将图像上传到 Firebase 存储?

angularjs - Firebase Storage 如何复制/复制存储的图像?

android - private void sendRegistrationToServer(String token) 参数 token 从不使用

javascript - 在 firebase web 中读取 child 的 child (javascript)

javascript - 使用 Firebase Push 键作为第二次推送中的键

Firebase CLI : the following filters were specified but do not match any functions in the project

android - 无法解析 : com. firebase :firebase-jobdispatcher:0. 5.0