title: How do I upload a base64 encoded image (string) directly to a Google Cloud Storage bucket using Node.js?
body: <p>Currently, I am using the <a href="https://www.npmjs.com/package/@google-cloud/storage" rel="noreferrer">@google-cloud/storage</a> NPM package to upload a file directly to a Google Cloud Storage bucket. This requires some trickery as I only have the image's base64 encoded string. I have to:</p>
  
  <ul>
  <li>Decode the string</li>
  <li>Save it as a file</li>
  <li>Send the file path to the below script to upload to Google Cloud Storage</li>
  <li>Delete the local file</li>
  </ul>
  
  <p>I'd like to avoid storing the file in the filesystem altogether since I am using Google App Engine and I don't want to overload the filesystem / leave junk files there if the delete operation doesn't work for whatever reason. This is what my upload script looks like right now:</p>
  
  <pre><code>// Convert the base64 string back to an image to upload into the Google Cloud Storage bucket
  var base64Img = require('base64-img');
  var filePath = base64Img.imgSync(req.body.base64Image, 'user-uploads', 'image-name');
  
  // Instantiate the GCP Storage instance
  var gcs = require('@google-cloud/storage')(),
      bucket = gcs.bucket('google-cloud-storage-bucket-name');
  
  // Upload the image to the bucket
  bucket.upload(__dirname.slice(0, -15) + filePath, {
      destination: 'profile-images/576dba00c1346abe12fb502a-original.jpg',
      public: true,
      validation: 'md5'
  }, function(error, file) {
  
      if (error) {
          sails.log.error(error);
      }
  
      return res.ok('Image uploaded');
  });
  </code></pre>
  
  <p>Is there anyway to directly upload the base64 encoded string of the image instead of having to convert it to a file and then upload using the path?</p>