简体   繁体   English

无法使用API​​从Google云端硬盘获取/下载文件

[英]Unable to get/download a file from Google Drive using the API

When I'm trying to get the file, my request goes through, but I end up with a file that is the correct type, but corrupt. 当我尝试获取文件时,我的请求通过了,但是最终得到的文件是正确的类型,但是损坏了。

The file contains: 该文件包含:

{
 "kind": "drive#file",
 "id": <ID of the File>,
 "name": <Name of the File>,
 "mimeType": <Type>
}

This is regardless of the file being an Image or Textfile. 这与文件是图像文件还是文本文件无关。

function getFile(auth, file, path, call) {
  console.log(file)
  var dest = fs.createWriteStream(path + "/" + file.name);

  const drive = google.drive({version: 'v3', auth});
  drive.files.get({fileId: file.id, mimeType: 'image/jpeg'}, {responseType: "stream"},
    function(err, res) {
       res.data
       .on("end", () => {
          console.log("Done");
       })
       .on("error", err => {
          console.log("Error", err);
       })
       .pipe(dest);
    })
}

How about this modification? 这个修改怎么样?

From: 从:

drive.files.get({fileId: file.id, mimeType: 'image/jpeg'}, {responseType: "stream"},

To: 至:

drive.files.get({fileId: file.id, alt: "media"}, {responseType: "stream"},

Note: 注意:

  • files.get method in Drive API cannot convert the mimeType. Drive API中的files.get方法无法转换mimeType。
  • If you want to export Google Docs (Spreadsheet, Document, Slides) by converting the mimeType, please modify above script as follows. 如果要通过转换mimeType导出Google文档(电子表格,文档,幻灯片),请按以下步骤修改上面的脚本。

     drive.files.export({fileId: file.id, mimeType: 'image/jpeg'}, {responseType: "stream"}, 

Reference: 参考:

If I misunderstood your question and this was not the result you want, I apologize. 如果我误解了您的问题,而这不是您想要的结果,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM