Node: Stream data from busboy to Google Drive

1.1k views Asked by At

I want to upload a file and then pass that file to google drive! I am using Busboy and Node Google Drive client!

I want to pass the file stream from busboy to the google drive API!

I dont want to create a temporary file and then create a fs.createReadStream to read that temporary file, I want it to be done purely using the memory, without any IO operation,

Any idea how to do it?

busboy.on('file', function(fieldname, file, filename, encoding, mimetype){
    console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);

    file.on('data', function(data){
        //Need to store all the data here and pass it back at file.on('end')            
    });

    file.on('end', function(){
        drive.files.insert({
            resource: {
                title: '123.jpg',
                mimeType: 'image/jpeg',
                parents: [{
                    kind: "drive#fileLink",
                    id: "0B0qG802x7G4bM3gyUll6MmVpR0k"
                }]
            },
            media: {
                mimeType: 'image/jpeg',
                body: //how to pass stream from busboy to here???
            }
        }, function(err, data){

        });
    })
})
1

There are 1 answers

0
mscdex On BEST ANSWER

According to the googleapis module documentation, body can be set to a Readable stream or a string. So your code might look something like this:

busboy.on('file', function(fieldname, file, filename, encoding, mimetype){
  console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);

  drive.files.insert({
    resource: {
      title: '123.jpg',
      mimeType: 'image/jpeg',
      parents: [{
        kind: "drive#fileLink",
        id: "0B0qG802x7G4bM3gyUll6MmVpR0k"
      }]
    },
    media: {
      mimeType: 'image/jpeg',
      body: file
    }
  }, function(err, data){

  });
})