如何从谷歌驱动器下载动态文件 [英] How to download dynamic files from google drive

查看:48
本文介绍了如何从谷歌驱动器下载动态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题,我刚刚开始使用Google Drive API v3.当我只有fileId时,如何从Google驱动器下载动态文件.文件可以是,图片,pdf或docs.

noob question, I'm just getting started with Google Drive API v3. How can I download dynamic file from google drive when I only have fileId. file can be, image, pdf, or docs.

我尝试搜索,但找不到与此相关的任何参考或示例.

I tried searching but I couldn't found any reference or example related to this.

到目前为止,我只下载了特定的文件扩展名.

This what I have so far but it only download specific file extension.

downloadFile(req, res) {
    const auth = new google.auth.JWT(
       client_email,
       null,
       private_key,
      SCOPES,
    );
    const { fileId } = req.params;
    const drive = google.drive({ version: 'v3', auth});
    var dest = fs.createWriteStream('./tmp/downloads/dummy.pdf')
    
    drive.files.get({
      fileId,
      alt: 'media',
    }, { 
      responseType: 'stream' 
    }).then((driveResponse) => {
      driveResponse.data.on('end', () => {
       console.log(`downloading fileID ${fileId}`);
      })
      .on('error', (err) => {
       console.log(err);
      })
      .on('data', (d) => {
        console.log(d);
      })
      .pipe(dest)
    })
    .catch((err) => {
      console.log(err);
    })
  }

是否可以从Google驱动器下载动态文件?

Is there way to download dynamic files from google drive?

推荐答案

我相信您的目标如下.

  • 您要使用服务帐户和文件ID从Google云端硬盘下载文件.
  • 文件包括Google Docs文件和Google Docs文件以外的文件.
  • 您想使用适用于Node.js的googleapis实现此目标.
  • 很遗憾,从它仅下载特定的文件扩展名.,我无法理解您的情况的详细信息.但是我想您出现问题的原因可能是由于同时下载了Google Docs文件和Google Docs文件以外的文件.
  • 下载Google Docs文件时,需要使用文件:导出"方法下载文件.在云端硬盘API中.
  • 当下载除Google Docs文件之外的文件时,要求使用文件:获取"方法下载文件.在云端硬盘API中.
  • 我认为上述情况可能是您遇到问题的原因.
  • 为了同时下载Google Docs文件和Google Docs文件以外的文件,我提出以下流程.
  • Unfortunately, from it only download specific file extension., I cannot understand about the detail of your situation. But I guess that the reason of your issue might be due to downloading both Google Docs files and the files except for Google Docs files.
  • When Google Docs files are downloaded, the files are required to be downloaded using the method of "Files: export" in Drive API.
  • When the files except for Google Docs files are downloaded, the files are required to be downloaded using the method of "Files: get" in Drive API.
  • I thought that above situation might be the reason of your issue.
  • In order to download both Google Docs files and the files except for Google Docs files, I propose the following flow.
  1. 检查文件ID的mimeType.
  2. 使用mimeType的每种方法下载文件.

当以上几点反映到您的脚本中时,它如下所示.

When above points are reflected to your script, it becomes as follows.

var dest = fs.createWriteStream('./tmp/downloads/dummy.pdf')

drive.files.get({
  fileId,
  alt: 'media',
}, { 
  responseType: 'stream' 
}).then((driveResponse) => {
  driveResponse.data.on('end', () => {
   console.log(`downloading fileID ${fileId}`);
  })
  .on('error', (err) => {
   console.log(err);
  })
  .on('data', (d) => {
    console.log(d);
  })
  .pipe(dest)
})
.catch((err) => {
  console.log(err);
})

收件人:

drive.files.get({ fileId, fields: "*" }, async (err, { data }) => {
  if (err) {
    console.log(err);
    return;
  }
  let filename = data.name;
  const mimeType = data.mimeType;
  let res;
  if (mimeType.includes("application/vnd.google-apps")) {
    const convertMimeTypes = {
      "application/vnd.google-apps.document": {
        type:
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        ext: ".docx",
      },
      "application/vnd.google-apps.spreadsheet": {
        type:
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        ext: ".xlsx",
      },
      "application/vnd.google-apps.presentation": {
        type:
          "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        ext: ".pptx",
      },
    };
    filename += convertMimeTypes[mimeType].ext;
    res = await drive.files.export(
      {
        fileId,
        mimeType: convertMimeTypes[mimeType].type,
      },
      { responseType: "stream" }
    );
  } else {
    res = await drive.files.get(
      {
        fileId,
        alt: "media",
      },
      { responseType: "stream" }
    );
  }
  const dest = fs.createWriteStream(filename);
  res.data
    .on("end", () => console.log("Done."))
    .on("error", (err) => {
      console.log(err);
      return process.exit();
    })
    .pipe(dest);
});

注意:

  • 在此修改中,我在 convertMimeTypes 处准备了3种类型的Google Docs文件.当您要下载其他mimeType时,请修改 convertMimeTypes .例如,在这种情况下,Google Docs文件将作为Microsoft Docs文件下载.
  • Note:

    • In this modification, I prepared 3 types of Google Docs files at convertMimeTypes. When you want to download other mimeTypes, please modify convertMimeTypes. In this case, for example, Google Docs files are downloaded as Microsoft Docs files.
    • 这篇关于如何从谷歌驱动器下载动态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆