Node.js Google Drive api Download file error

不羁岁月 提交于 2020-01-11 05:43:11

问题


This is my code:

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;

Following the google documentation 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

TypeError: Cannot read property 'on' of undefined

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?

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.

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

Thanks for any advice.


回答1:


From your script, I thought that you may be using the googleapis of the version more than v24.0.0. So how about modifying the version of googleapis? Also I have experienced the same issue with your situation. In my case, the issue was removed by modifying the version of googleapis to v24.0.0.

When I used v25.0.0 - v30.0.0 of googleapis, the error occurs. When it used the googleapis of v24.0.0, the error was removed.

Note :

  • For the googleapis after the version of v25.0.0, some bugs for APIs and the options are reported. I believe that these bugs are removed in the future. So if for APIs and the options you use, some errors occur, please modify the version of googleapis, and try again. The cases which were solved by modifying the version are as follows.

References :

  • How do I update my google sheet in v4?
  • Create a gmail filter with Gmail API nodejs, Error: Filter doesn't have any criteria
  • Insufficient Permission when trying to create a folder on Google Drive via API(v3)
  • Youtube Data API V3 - Error fetching video with google.youtube.videos.list()
  • Google drive API - Cannot read property 'OAuth2' of undefined
  • How to run a Google App Script using Google API Service Library (Node.js)

If this was not useful for your situation, I'm sorry.

Edit :

How about this modification? In my environment, I confirmed that this modification worked by googleapis of v30.0.0.

From :

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

To :

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



回答2:


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

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

module.exports = gotData;


来源:https://stackoverflow.com/questions/50373224/node-js-google-drive-api-download-file-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!