0

I am trying to work on a function to download a google file straight to memory. It works for downloading it to a file but I want to skip that step and hold the contents in memory. Can someone please lend a few ideas on how to handle the piping into a string or buffer? Here is my code below

function downloadFile(){
  var fileId = "xxxxxx"
  var testStr = ""//was trying to write it to a string
  var dest = fs.createWriteStream(testStr);

  drive.files.export({
     auth: oauth2Client,
     fileId: fileId,
     mimeType: 'text/csv'
  })
  .on('end', function() {
    console.log('Done Dowloading' + testStr);
  })
  .on('error', function(err) {
    console.log('Error during download', err);
  })
  .pipe(dest);
  dest.on('finish', function() {
      dest.close((evt)=>{console.log(evt)});  // close() is async, call cb after close completes.
    })
  dest.on('error',function (err) {
    console.log("error in pipe of dest \n" + err)
  })
}

I was trying to download it to a string and I couldn't figure out how to make a buffer with a dynamic byte allocation. To recap it works for passing an actual file into createWritableStream but I want it to go straight to memory.

Peter3
  • 2,549
  • 4
  • 20
  • 40

1 Answers1

-1

Perhaps the easiest way is to use a module like stream-buffers to replace your file stream with:

const streamBuffers = require('stream-buffers');
...
let dest = new streamBuffers.WritableStreamBuffer();

To access the data:

dest.on('finish', () => {
  let data = dest.getContents();
  ...
});
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • I get an error saying that createWriteStream() is not a constructor. I'll try looking more into the package though – Peter3 Mar 14 '17 at 21:30
  • @Peter3 sorry, my bad, I fixed my answer :) – robertklep Mar 14 '17 at 21:31
  • thanks I just found the write syntax on npm. When I print data out it gives me a buffer that prints out everything into hexadecimal bytes. Do you have any experience with now converting that back into "english" – Peter3 Mar 14 '17 at 21:36
  • So there is another function the package exposes that I used for this. Thanks so much. let data = dest.getContentsAsString(); – Peter3 Mar 14 '17 at 21:45