Node.js Google Drive api 下载文件错误 [英] Node.js Google Drive api Download file error

查看:20
本文介绍了Node.js Google Drive api 下载文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

const fs = require('fs');
const { google } = require('googleapis');
const Auth = require('../authorization/auth_drive/auth');

// get data
var gotData = function downloadFiles(fileId, callback) {
    let content = fs.readFileSync('./GEARS/authorization/client_secret.json');
    Auth(JSON.parse(content), (auth) => {
        //const returnData = [];
        const drive = google.drive({ version: 'v3', auth });

        const fileId = '';
        const dest = fs.createWriteStream('./test_files/test.png');

        drive.files.get({fileId: fileId, alt: 'media'})
        .on('end', () => {
            console.log('Done');
        })
        .on('error', err => {
            console.log('Error', err);
        })
        .pipe(dest);
    });
};

module.exports = gotData;

遵循谷歌文档LINK

我尝试过的所有其他功能,如列表、复制、上传或表格读写,对我来说都非常适合,除了下载.我的应用程序立即崩溃并出现错误

All other functions I have tried, like List, Copy, Upload or Sheets Read&Write work perfectly for me, except for the Download. My application instantly crashes with the error

类型错误:无法读取未定义的属性on"

TypeError: Cannot read property 'on' of undefined

我已经查看了有关此功能的 Google 示例,但没有发现任何不同之处,那么为什么它对 Google Developers 有效,而对我而言却无效?

I have looked at Google's Sample of this very functionality, and I have not found anything that differs, so why does it work for Google Developers, while for me it doesn't?

我在 google 上还没有发现一个人有这个问题,所以也许,如果你明白应该怎么做,你就能看到我做错了什么.

I have not found a single person with this problem on google, so maybe, if you understand what should be done, you can see what I am doing wrong.

-- 我将包含 THIS LINK,这是我拥有的复制功能,可以完美运行,以防万一这将是相关的.

-- I will include THIS LINK, that is a Copy function I have, which works perfectly, in case it would be relevant.

感谢您的建议.

推荐答案

我修改了 Google 的示例.它是这样工作的:

I have altered Google's example. This way it works:

const fs = require('fs');
const { google } = require('googleapis');
const Auth = require('../authorization/auth_drive/auth');

// get data
var gotData = function downloadFiles(fileId, destination, callback) {
    let content = fs.readFileSync('./GEARS/authorization/client_secret.json');
    Auth(JSON.parse(content), (auth) => {
        const returnData = [];
        const drive = google.drive({ version: 'v3', auth });

        const file = fs.createWriteStream(destination); // destination is path to a file

        drive.files.get(
        {fileId: fileId, alt: 'media',}, 
        {responseType: 'stream'}, (err,  res ) => {
            if (err) {
                returnData.push(["ERR"]);
                returnData.push("" + err);
            } else {
                res.data.pipe(file);
                returnData.push("Downloaded");
            }
            callback(returnData);
        });
    });
};

module.exports = gotData;

这篇关于Node.js Google Drive api 下载文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆