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

nickparsons

Nick Parsons

Posted on May 29, 2019

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!


💖 💪 🙅 🚩
nickparsons
Nick Parsons

Posted on May 29, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related