如何从谷歌驱动器下载动态文件

bli*_*n12 2 javascript node.js express google-drive-api

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

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

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

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);
    })
  }
Run Code Online (Sandbox Code Playgroud)

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

Tan*_*ike 6

我相信你的目标如下。

  • 您想使用服务帐户和文件 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中的“文件:获取”方法下载文件。
  • 我认为上述情况可能是您出现问题的原因。
  • 为了同时下载 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);
})
Run Code Online (Sandbox Code Playgroud)

到:

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);
});
Run Code Online (Sandbox Code Playgroud)

笔记:

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

参考: