【问题标题】:Export google drive file in nodeJS在nodeJS中导出谷歌驱动器文件
【发布时间】:2020-01-22 13:37:30
【问题描述】:

我想导出 google drive 文件,但我以前不知道文件类型。我使用了 drive.files.export 但它需要我不知道的 mimeType。 drive.files.get 中的 mimeType 不能用于导出。如果我只有文件 ID,如何下载文件?

【问题讨论】:

  • 我不确定我是否理解您的问题。您不知道要以哪种 mimeType 导出文件?另外,它是 G Suite 文档吗?
  • 我不知道它将是什么类型的文件,因为用户可以选择文件。但我设法解决了它。似乎 mimeType 'application/vnd.google-apps.document' 不能用于导出,但 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' 可以。

标签: javascript node.js file google-drive-api export


【解决方案1】:

根据文件是否为 G Suite 文档,您应遵循的下载过程有所不同。

如果文件是a G Suite document,您必须使用files.export 并指定要将文件导出到哪个mimeType。 See here 参考不同的 Google 文档和支持的导出 MIME 类型。如果您想根据文件在 Drive 中的类型将文件导出为不同的 MIME 类型,您可以先执行files.get,然后根据文件的 mimeType 和引用的表,将其导出为相应的类型。例如,如果您的文件的 MIME 类型是 application/vnd.google-apps.spreadsheet,您可能希望将其导出到 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

如果文件不是 G Suite 文档,则必须使用files.get 下载并将参数alt 设置为media,如in the official documentation 所述。这是与在 Node.js 中执行此操作相对应的代码 sn-p,在此处指定:

var fileId = 'your-file-id';
var dest = fs.createWriteStream('/tmp/photo.jpg');
drive.files.get({
  fileId: fileId,
  alt: 'media'
})
    .on('end', function () {
      console.log('Done');
    })
    .on('error', function (err) {
      console.log('Error during download', err);
    })
    .pipe(dest);

所以,总结一下,我要做的是,首先,通过files.get 获取要导出的文件的 mimeType,并基于此 mimeType(取决于文件是否为 G Suite 文档和哪种类型的文档),以一种或另一种方式下载文件。

参考:

希望对你有帮助。

【讨论】:

  • 谢谢。我正在使用这种方式,但正如我在之前的评论中所说,某些 mimeType 不能用于导出。不过还是谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多