DEV Community

Nick Parsons
Nick Parsons

Posted on • Originally published at hackernoon.com on

Streaming Images & Videos to Google Cloud Storage with Node.js, Express, and Multer

The Problem 😒

I was tasked with transitioning code that's primary focus was to upload high-resolution images and large video files. It had to be highly reliable and more importantly, no longer live on AWS S3. I’m not going to get into the details of S3 vs. other storage providers, but I will say that Google’s infrastructure was extremely pleasing to work me when I chose to use Cloud Storage — a beautiful UI, amazing file explorer, and dozens of options that actually make sense (to me) opposed to S3.

One problem… Google has almost zero examples for uploading images that are NOT on disk to Cloud Storage. And, if they do have them, they appeared to be way out of date for my liking. After a giant headache and hours of researching StackOverflow, looking at the SDKs source code, etc., I can now say that I’ve accomplished the task… I can finally upload images using Express.js and Multer, and I’m happy to share the code that makes it all work.

Requirements 📜

  1. Upload any media file (audio, video, image, etc.)
  2. Custom UUID for the name
  3. Append proper extension to end
  4. Get MIME type to pass to Google Cloud Storage
  5. Set object ACL to “ public” so that we can view in the browser

Solution 🚨

Express, Multer, and the Google Cloud Storage package for Node.js. See below for code examples:

1. Route File 🚓

2. Controller 🚂

3. Config 🛠

That’s it! 👏

Pretty simple, right? If you have any questions about your implementation, drop them in the comments below or hit me up on Twitter. I’m always happy to help!


Top comments (6)

Collapse
 
amitgiri0001 profile image
amit giri

Hey, this looks really good but it seems to me that the code is going to store data in the buffer before it actually starts steaming the buffer to the gcs bucket.

This means that if the limit not set in the multer options the app will run out of memory if the filesize is huge.
Let us know what you think. Cheers :+1

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
aarabmed profile image
AARAB MOHAMMED

he did just the first part which is saving the image on google cloud storage, the second part ,hypothetically he suppose to be saving the path of the image in the database, so when he does fetch the images he gets the list of paths and then assign each one of them to an img tag.

Collapse
 
bhaveshfides profile image
bhavesh-fides

Help me i want to store the files in to specifc folders.

Collapse
 
mufasa71 profile image
Shukhrat Mukimov

Why do we need to store extension?

Collapse
 
emmabidem profile image
Oluwatobi Immanuel

Please why can't i see the rest of this content?