Upload JSON string to Google Cloud Storage without a file

In case some one is looking for the snippet of the answer here it is using the file.save(). Please do note that the data should be stringfy.

const storage = new Storage();

exports.entry_point = (event, context) => {
  var data = Buffer.from(event.data, 'base64').toString();
  data = transform(data)
  var datetime = new Date().toISOString()
  var bucketName =  storage.bucket('your_filename')
  var fileName = bucketName.file(`date-${datetime}.json`)
  fileName.save(data, function(err) {
  if (!err) {
    console.log(`Successfully uploaded ${fileName}`)
  }});

  //-
  // If the callback is omitted, we'll return a Promise.
  //-
  fileName.save(data).then(function() {});
};

It looks like this scenario has already been addressed in this other question.

Both answers look good, but it will probably be easier to use the file.save() method (second answer).You can find the description of this method and yet another example here.

Hope this helps.


expanding on @ch_mike's answer

const { Storage } = require('@google-cloud/storage')
const storage = new Storage()
const bucket = storage.bucket(bucketName)

const saveJsonFile = (data) => {
   const timestamp = new Date().getTime()
   const fileName = `${timestamp}.json`
   const file = bucket.file(fileName)
   const contents = JSON.stringify(data)
   return file.save(contents)
}