最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 3浏览 0评论

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

我在使用v3 REST API从Google云端硬盘下载文件时遇到了困难。我正在通过axios发出请求,并获取数组缓冲区作为响应,但是,保存到文件中的缓冲区已损坏。

这些是我不断提出请求的响应头:

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}

我猜测gzip压缩和utf-8编码使文件混乱。我还通过浏览器下载了相同的文件,并比较了它们的原始内容,当然两个文件的内容肯定不同。任何帮助将不胜感激。

编辑:

这是我当前正在以上述标题和文件内容作为响应的请求方式。

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

正如@Suhakar RS所说,您可以查看this example以了解如何做。

这是相关部分:

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

注意文件如何作为流被请求并以相同格式写入。

希望这会有所帮助!

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

我在使用v3 REST API从Google云端硬盘下载文件时遇到了困难。我正在通过axios发出请求,并获取数组缓冲区作为响应,但是,保存到文件中的缓冲区已损坏。

这些是我不断提出请求的响应头:

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}

我猜测gzip压缩和utf-8编码使文件混乱。我还通过浏览器下载了相同的文件,并比较了它们的原始内容,当然两个文件的内容肯定不同。任何帮助将不胜感激。

编辑:

这是我当前正在以上述标题和文件内容作为响应的请求方式。

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

正如@Suhakar RS所说,您可以查看this example以了解如何做。

这是相关部分:

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

注意文件如何作为流被请求并以相同格式写入。

希望这会有所帮助!

发布评论

评论列表 (0)

  1. 暂无评论