Adhil
Adhil

Reputation: 1858

Node.js: Unable to upload to FirebaseStorage

Hi I am trying to upload an image to my firebase storage:

this is my code:

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);
};

However I am getting the following error:

>  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)

I have checked the "C:\Users\adhil\AppData\Local\Temp\8550820.jpeg" and found it to be a 0 MB file. which means that it has not been copied to my local directory successfully.

please help me to understand why fs.createWriteStream is not working properly as I have reinstalled the module multiple times. I am new to node.js so I hope you can help me out.

Upvotes: 0

Views: 216

Answers (1)

Salman Hanif
Salman Hanif

Reputation: 24

Was stuck at this same issue. It's working now in the latest update of firebase-tools 7.1.0. https://github.com/firebase/firebase-tools/issues/1447

Upvotes: 1

Related Questions