Tuesday 14 March 2017

Download google file to memory.

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.



via Peter3

No comments:

Post a Comment