Search code examples
node.jsgcloud

load a gcloud createReadStream into a variable in nodejs


I'm using the nodejs API for cloud storage

https://github.com/GoogleCloudPlatform/gcloud-node

I struggle in find a way to load a file as a string using the createReadStream().

There is an example on how to pipe the file and write in the local environment, but I'd prefer to just load it into a variable.


Solution

  • For future reference, This how I solved it. Fetching data from the buffer continuously.

    var gcloud = require("gcloud")({ /* credentials. */ });
    var cloudstorage = gcloud.storage();
    var myTextFile = cloudstorage.file('your-file.txt');
    
    var fileContents = new Buffer('');
    
    myTextFile.createReadStream()
      .on('data', function(chunk) {
        fileContents = Buffer.concat([fileContents, chunk]);
      })
      .on('end', function() {
        // `fileContents` is ready
      });
    

    The gCloud 3rd party libraries team generously agree in adding a new methods for simplify the request:

    https://github.com/GoogleCloudPlatform/gcloud-node/pull/381

    Update:

    The function is now available in the gcloud library.