2

I am using the multer package to handle uploading files to a google cloud bucket. Initially, my multer package was working correctly but once I migrated to firebase I found that all my attempts to upload files were not resolving.

After running firebase functions:log I found I was receiving the message Error: Unexpected end of form at Multipart._final I'm not totally sure why this error may be occurring especially since it was previously working.

This question seems to have the same issue however there don't seem to be any answers which help me. If possible I would like to know what the error message itself means as well.

Below is my code for invoking my multer function

 const sendFile = () =>{
        if(acceptedFiles.length > 0){
            for(let i = 0; i < acceptedFiles.length; i++){
              let file = acceptedFiles[i]
      
              let blob = file.slice(0,file.size)
              let newFile = new File([blob], file.name, {type: "text/plain"});
      
              let formData = new FormData();
              formData.append('dataFile',newFile)
      
      
              fetch('https://somePath',{
                method: "Post",
                body: formData
              })
                .then(res => res.text())
                .then((x) => console.log(x))
                .then(() => sendDB(acceptedFiles[i],i))
            }
          }else{
            window.alert("No files have been selected to be uploaded")
          }
    }

Here is where I actually send my file to my google bucket

app.post('/uploadFile', multer.single('dataFile'),async (req,res) =>{

    console.log('made it to upload')
    try{
        if(req.file){
            console.log('file found trying to upload....')
            const blob = bucket.file(req.file.originalname);
            const blobstream = blob.createWriteStream();

            blobstream.on('finish',() => {
                res.status(200).send('Success')
            })
            blobstream.end(req.file.buffer);
        }
    }catch(error){
        res.status(500).send(error);
        console.log(error)
    }
})

Here is how I configured my multer and google cloud storage

const multer = Multer({
    storage: Multer.memoryStorage()
})

let projectId = 'someID'
let keyFilename = 'someKeyFileName'

const storage = new Storage({
    projectId,
    keyFilename
})

I'm more than happy to provide any additional information if it's needed.

Update:

After some more research, it seems that firebase does not support serverside file upload. Is this only when using firebase storage or will this also apply to my case where I am attempting to upload my files to a google cloud bucket? Reference to this claim in case I'm incorrect.

1 Answers1

0

I'm not sure if it's your case, because I was getting the same error, but without firebase, however in my case the problem was Multer itself. Dowgraded to 1.4.3 and it started working.

See https://github.com/expressjs/multer/issues/1144

Lucio Crusca
  • 1,277
  • 3
  • 15
  • 41
  • 2
    Yes the issue is firebase does not support multer due to how firebase pre-processes requests. Therefore the solution is to use a low level library like busboy or google itself has an easier solution using a newer function called .save. However, based on the number of questions I've found on stackoverflow and the answers those questions get I would say it's poorly documented. – boredProjects Jan 18 '23 at 16:33