简体   繁体   English

如何使用 Google Drive API REST 下载文件?

[英]How to Download files using Google Drive API REST?

I am having difficulties in downloading files from Google Drive using the v3 REST API.我在使用 v3 REST API 从 Google Drive 下载文件时遇到困难。 I am making the request via axios and getting the array buffer as a response, however, the buffer when saved to a file is corrupted.我通过 axios 发出请求并获取数组缓冲区作为响应,但是,保存到文件时的缓冲区已损坏。

These are the response headers that i am getting on making the request:这些是我在发出请求时收到的响应标头:

HTTP/1.1 200 date: Mon, 07 Oct 2019 21:36:21 GMT
content-encoding: gzip
expires: Mon, 07 Oct 2019 21:36:21 GMT
server: GSE
content-type: text/plain; charset=UTF-8
vary: Origin, X-Origin
cache-control: private, max-age=0, must-revalidate, no-transform
Content-Type: application/pdf
{file content}

i am guessing the gzip compression and utf-8 encoding is what is messing up with the file.我猜是 gzip 压缩和 utf-8 编码是什么弄乱了文件。 I also downloaded the same file via the browser and compared their raw contents and surely the content differs in both the files.我还通过浏览器下载了相同的文件并比较了它们的原始内容,当然这两个文件的内容都不同。 Any help will be appreciated.任何帮助将不胜感激。

EDIT:编辑:

This is how I am making the request currently for which I am getting the above headers and file contents as a response.这就是我当前发出请求的方式,我正在为此获取上述标头和文件内容作为响应。

const {data} = await axios.get(`${exportLink}&${qs.stringify({access_token: access_token})}`, {responseType: 'arraybuffer',
        timeout: 30000,})

As @Suhakar RS said, you can look at this example to learn how to do it.正如@Suhakar RS所说,您可以查看此示例以了解如何操作。

This is the relevant part:这是相关部分:

drive.files
  .get({fileId, alt: 'media'}, {responseType: 'stream'})
  .then(res => {
    return new Promise((resolve, reject) => {
      const filePath = path.join(os.tmpdir(), uuid.v4());
      console.log(`writing to ${filePath}`);
      const dest = fs.createWriteStream(filePath);
      let progress = 0;

      res.data
        .on('end', () => {
          console.log('Done downloading file.');
          resolve(filePath);
        })
        .on('error', err => {
          console.error('Error downloading file.');
          reject(err);
        })
        .on('data', d => {
          progress += d.length;
          if (process.stdout.isTTY) {
            process.stdout.clearLine();
            process.stdout.cursorTo(0);
            process.stdout.write(`Downloaded ${progress} bytes`);
          }
        })
        .pipe(dest);
    });
  });

Notice how the file is requested as a stream and is written in the same format.请注意文件是如何被请求为 stream 的,并以相同的格式写入。

Hope this helps!希望这可以帮助!

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

相关问题 如何使用 API 从谷歌驱动器下载文件作为 Blob - How to download a file as Blob from google drive using API 如何从谷歌驱动器下载动态文件 - How to download dynamic files from google drive 使用列表数据为 drive.files.export Google Drive API V3 使用 Nodejs 定义下载参数 - Using list data to define download parameters for drive.files.export Google Drive API V3 using Nodejs 如何使用带有节点 js 的谷歌驱动器 api 列出谷歌团队驱动器中的所有文件 - How to List all the files from google team drive using google drive api with node js 下载 torrent 文件到谷歌驱动器或兆驱动器 - to download torrent files to google drive or mega drive 如何查看Google Drive API的文件和文件夹? - How to see files and folder of google drive API? 无法使用API​​从Google云端硬盘获取/下载文件 - Unable to get/download a file from Google Drive using the API 使用 REST API 从 URL 上传文件到 Google Drive - Upload file to Google Drive from URL using REST API 如何仅使用Google云端硬盘的REST API检查访问令牌是否有效? - How do I check if an access token is valid using only Google Drive's REST API? 如何在Node.js中从Google Drive API下载文件mp4 - How to download file mp4 from google drive api in nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM