首页 > 解决方案 > Node.js:无法上传到 FirebaseStorage

问题描述

嗨,我正在尝试将图像上传到我的 firebase 存储:

这是我的代码:

exports.uploadImage = (req, res) => {
    const BusBoy = require('busboy');
    const path = require('path');
    const os = require('os');
    const fs = require('fs');

    const busboy = new BusBoy({ headers: req.headers});

    let imageFileName;
    let imageToBeUploaded = {};

    busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {

        console.log(fieldname);
        console.log(filename);
        console.log(mimetype);
        //image.png
        const imageExtension = filename.split('.')[filename.split('.').length - 1];
        
        imageFileName = `${Math.round(Math.random()*10000000).toString()}.${imageExtension}`;
        
        const filepath = path.join(os.tmpdir(), imageFileName);
        console.log(filepath);
        imageToBeUploaded = {filepath, mimetype};
        file.pipe(fs.createWriteStream(filepath));
    });
    busboy.on('finish', () => {
        admin.storage().bucket().upload(imageToBeUploaded.filepath,{
            resumable: false,
            metadata: {
                metadata: {
                    contentType: imageToBeUploaded.mimetype
                }
            }
        })
        .then(() => {
            const imageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`;
            console.log(imageUrl);
            return db.doc(`/users/${req.user.handle}`).update({ imageUrl: imageUrl});
        })
        .then(() => {
            return res.json({message: 'Image uploaded successfully'});
        })
        .catch(err => {
            console.error(err);
            return res.status(500).json({error: err.code})
            
        });
    });
    busboy.end(req.rawBody);
};

但是我收到以下错误:

>  image
>  image111.jpeg
>  image/jpeg
>  C:\Users\adhil\AppData\Local\Temp\8550820.jpeg
>  events.js:174
>        throw er; // Unhandled 'error' event
>        ^
> 
>  Error: Unexpected end of multipart data
>      at C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\dicer\lib\Dicer.js:61:28
>      at process._tickCallback (internal/process/next_tick.js:61:11)
>  Emitted 'error' event at:
>      at Busboy.emit (C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\busboy\lib\main.js:37:33)
>      at Dicer.<anonymous> (C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\busboy\lib\types\multipart.js:282:9)
>      at Dicer.emit (events.js:198:13)
>      at Dicer.emit (C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\dicer\lib\Dicer.js:79:35)
>      at C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\dicer\lib\Dicer.js:61:14
>      at process._tickCallback (internal/process/next_tick.js:61:11)

我检查了“C:\Users\adhil\AppData\Local\Temp\8550820.jpeg”,发现它是一个 0 MB 的文件。这意味着它尚未成功复制到我的本地目录。

请帮助我了解为什么 fs.createWriteStream 无法正常工作,因为我已多次重新安装该模块。我是 node.js 的新手,所以我希望你能帮助我。

标签: node.jsfirebase

解决方案


被困在同样的问题上。它现在在最新更新的 firebase-tools 7.1.0 中运行。https://github.com/firebase/firebase-tools/issues/1447


推荐阅读