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.
- Upload any media file (audio, video, image, etc.)
- Custom UUID for the name
- Append proper extension to end
- Get MIME type to pass to Google Cloud Storage
- Set object ACL to “ public” so that we can view in the browser
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!