node.jsgoogle-app-enginegcloudgcloud-node

Uploading a buffer to google cloud storage


I'm trying to save a Buffer (of a file uploaded from a form) to Google Cloud storage, but it seems like the Google Node SDK only allows files with a given path to be uploaded (Read / Write streams).

This is what I have used for AWS (S3) - is the anything else similar in the Google node SDK?:

var fileContents = new Buffer('buffer');

var params = {
  Bucket: //bucket name
  Key: //file name
  ContentType: // Set mimetype
  Body: fileContents 
};

s3.putObject(params, function(err, data) {
// Do something 
});

The only way that I have found to do it so far is write the buffer to disk, upload the file using the SDK (specifying the path to the new file) and then delete the file once it's uploaded successfully - the downside to this is that the whole process is significantly slower, to where it seems to be unfeasible to use Google storage. Is there any work around / way to upload a buffer?


Solution

  • We have an issue about supporting this more easily: https://github.com/GoogleCloudPlatform/gcloud-node/issues/1179

    But for now, you can try:

    file.createWriteStream()
      .on('error', function(err) {})
      .on('finish', function() {})
      .end(fileContents);