DEV Community

mich
mich

Posted on • Edited on

Shorter Way to Upload to Google Cloud Storage (Just Send the Buffer)

Part of my current project is to take a disk image that's uploaded by the user and place it in google cloud storage. This is something that seems like it should be straightforward and it started close to that then moved further in that direction.

I started with this excellent thorough writeup and picked out the stuff that helped flesh out the mechanics. I use express-fileupload instead of multer just because I like it better for no other objective reason I could really name other than the free md5 sum which I use to know which files are duplicates based on their content.

Many of the examples I was seeing used .createWriteStream() which is likely a really good "learn the long way first" route. However there's a much simplified version (which is the job of a library) called .save(). Your files don't have a content type, but that can be added with a google cloud function (especially if you're already going to do other stuff with it) or a second call to the API after the upload. Thanks to this thread for pointing out this shortcut over three years ago.

Below is code that's roughly what I use.

import Cloud from "@google-cloud/storage";  // currently @5.1.2
const { Storage } = Cloud;

// same storage object for all uploads
const storage = new Storage({
  "keyFilename": "./PATH/MY_GCP_KEY.json"
});

// same bucket for all uploads
const bucket = storage.bucket(MY_BUCKET);

const uploadDiskImage = async file => {
  const { filename, buffer } = file;
  const fileHandle = bucket.file(filename);
  const [ fileExists ] = await fileHandle.exists();
  if (fileExists === false) {
    return fileHandle.save(buffer);
  }
  return new Promise((resolve, reject) => resolve(filename));
};

export default uploadDiskImage;

I believe most of it is pretty simple to read which was largely the point of this post. It could probably get simplified quite a bit more (the .exists() feels like an area of opportunity), but for now it's not too shabby.

Top comments (0)