question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

My upload image gets stuck on "finish" event. It doesn't upload image on Firebase Storage

See original GitHub issue

I’m having an issue with uploading an image on Firebase Storage with Firebase functions. It all works until it gets to the the finish event of BusBoy. I console logged some arbitral word in finish event, but it doesn’t get called.

This is the 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) => {
   if (mimetype !== 'image/jpeg' && mimetype !== 'image/png') {
     return res.status(400).json({ error: 'Wrong file type submitted' })
   }
   const imageExtension = filename.split('.')[filename.split('.').length - 1]
   const imageFileName = `${Math.round(
     Math.random() * 10000000000000000
   )}.${imageExtension}`

   const filepath = path.join(os.tmpdir(), imageFileName)
   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://storage.cloud.google.com/${
         config.storageBucket
       }/${imageFileName}?alt=media`
       console.log(imageUrl)
       return db.doc(`/users/${req.user.handle}`).update({ imageUrl })
     })
     .then(() => {
       return res.json({ message: 'Image uploaded successfully' })
     })
     .catch(err => {
       console.log(err)
       return res.status(500).json({ error: err.code })
     })
 })
 busboy.end(req.rawBody)
}

“busboy”: “^0.3.1”, “dotenv”: “^8.0.0”, “firebase”: “^6.2.3”, “firebase-admin”: “^8.0.0”, “firebase-functions”: “^3.0.0”

Link to the GithHub repo: https://github.com/rades12340/socialape/blob/master/functions/handlers/users.js

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
Neveoncommented, Jul 2, 2019

I had to rollback firebase-tools.

In functions folder npm rm firebase-tools npm i -g firebase-tools@6.8.0 Then add config.storageBucket to the .bucket() in the 'finish' event handler

I can now sleep in peace knowing I defeated another bug

1reaction
rades12340commented, Jul 3, 2019

I had to rollback firebase-tools.

In functions folder npm rm firebase-tools npm i -g firebase-tools@6.8.0 Then add config.storageBucket to the .bucket() in the 'finish' event handler

I can now sleep in peace knowing I defeated another bug

This worked. Thanks. I couldn’t imagine it was because of this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upload files with Cloud Storage on Web - Firebase
Cloud Storage for Firebase allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase.
Read more >
firebase upload stuck at 0.0% - no error given - Stack Overflow
Compresses an image on the local disk and puts it into a cache folder. (using sharp.js); Uploads the image to a location in...
Read more >
Slow rendering - Android Developers
If your app suffers from slow UI rendering, then the system is forced to skip frames and the user will perceive stuttering in...
Read more >
I can't display an image in app from firebase storage using the ...
I got the user to pick an image from gallery and saved it on the database. I then saved the URL link in...
Read more >
Troubleshooting Cloud Functions - Google Cloud
This role is required for Cloud Pub/Sub, IAM, Cloud Storage and Firebase ... this image when it needs to run the container to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found