节点:将数据从busboy流式传输到Google云端硬盘

时间:2014-11-11 07:18:23

标签: node.js google-drive-api busboy

我想上传文件,然后将该文件传递给google驱动器! 我正在使用Busboy和Node Google Drive客户端!

我想将文件流从busboy传递到google驱动器API!

我不想创建一个临时文件,然后创建一个fs.createReadStream来读取该临时文件,我希望它完全使用内存完成,无需任何IO操作,

知道该怎么做吗?

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 个答案:

答案 0 :(得分:1)

根据googleapis module documentationbody可以设置为可读流字符串。所以你的代码看起来像这样:

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){

  });
})