Google 驱动器中的下载进度

Downloading progress in Google Drive

在我的 Meteor 服务器应用程序中,我正在使用此代码从 Google 驱动器下载文件,

var dest = fs.createWriteStream('/data/'+data.name);
        drive.files.get({
           fileId: data.id,
           alt: 'media',
           auth: jwtClient
        })
        .on('end', Meteor.bindEnvironment(function() {

        }))
        .on('error', function(err) {
          console.log('Error during download', err);
        })
        .pipe(dest);

如何获取下载进度?例如,我希望每 30 秒使用 console.log()

显示下载进度

我可以使用 .on('data') 吗?我正在使用 google 由 Google 提供的驱动器 nodejs v3。

您可以从 drive.files.list 中获取带有文件名的 File meta (id, name, size),然后您就可以下载文件了。

使用 Node.js google 驱动器 的快速入门以进行身份​​验证。

我正在使用 progress-stream 来衡量收到的数据百分比。

var callAfterDownload = function (fileName, callback) {
  drive.files.list({
    auth: oauth2Client,
    pageSize: 1,
    q: 'name=\'' + fileName + '\'',
    fields: 'nextPageToken, files(id, name, size)'
  }, function (err, response) {
    if (err) {
      console.log('The API returned an error: ' + err)
      callback(['Error while download'])
    } else {
      var files = response.files
      //when only one file is matched we will download
      if (files.length === 1) {

        var file = files.pop()
        console.log('%s (%s)', file.name, file.id)
        var dest = fs.createWriteStream(file.name)
        var progress = Progress({time:100, length: file.size})

        //downloading matched file from drive
        drive.files.get({
          auth: oauth2Client,
          fileId: file.id,
          alt: 'media'
        }).on('error', function (err) {
          console.log('Error during download', err)
          callback(['Error while download'])
        }).pipe(progress).pipe(dest)

        //checking progress of file
        progress.on('progress', function(progress) {
          console.log('download completed ' +progress.percentage.toFixed(2) + '%')
        });

        //when write stream has finally written to file
        dest.on('finish', callback)

      } else {
        console.log('EXITING......More than one/no file exist with same name, make sure you have unique file name.')
        callback()
      }
    }
  })
}


function downloadDriveFile () {
  var fileName = 'testfile.doc'
  callAfterDownload(fileName, function (err) {
    if(err) throw err
    //your logic to do anything with the file
  })
}

downloadDriveFile();