【问题标题】:How to download dynamic files from google drive如何从谷歌驱动器下载动态文件
【发布时间】:2020-10-13 15:02:12
【问题描述】:

菜鸟问题,我刚刚开始使用 Google Drive API v3。当我只有 fileId 时,如何从谷歌驱动器下载动态文件。文件可以是图像、pdf 或文档。

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

这是我目前所拥有的,但它只下载特定的文件扩展名。

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);
    })
  }

有没有办法从谷歌驱动下载动态文件?

【问题讨论】:

  • 您可以搜索以前的文件吗?要获取扩展程序?

标签: javascript node.js express google-drive-api


【解决方案1】:

我相信你的目标如下。

  • 您想使用服务帐户和文件 ID 从 Google Drive 下载文件。
  • 这些文件既包括 Google Docs 文件,也包括 Google Docs 文件以外的文件。
  • 您希望使用 googleapis for Node.js 实现此目的。

修改点:

  • 很遗憾,来自it only download specific file extension.,我无法了解您的具体情况。但我猜您的问题的原因可能是由于同时下载了 Google Docs 文件和 Google Docs 文件以外的文件。
  • 下载 Google Docs 文件时,需要使用 Drive API 中的“文件:导出”方法下载文件。
  • 下载Google Docs文件以外的文件时,需要使用Drive API中的“Files:get”方法下载文件。
  • 我认为上述情况可能是您的问题的原因。
  • 为了同时下载 Google Docs 文件和 Google Docs 文件以外的文件,我建议以下流程。
    1. 检查文件 ID 的 mimeType。
    2. 通过 mimeType 使用每种方法下载文件。

当以上几点反映到你的脚本中时,它变成如下。

修改后的脚本:

发件人:

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文件。如需下载其他mimeTypes,请修改convertMimeTypes。例如,在这种情况下,Google Docs 文件将作为 Microsoft Docs 文件下载。

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2018-07-21
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多