Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load a gcloud createReadStream into a variable in nodejs

Tags:

node.js

gcloud

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.

like image 228
Snick Avatar asked Oct 30 '25 05:10

Snick


1 Answers

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.

like image 136
Snick Avatar answered Nov 02 '25 23:11

Snick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!