如何从Google Drive下载mp4文件

时间:2019-03-29 06:26:38

标签: google-drive-api google-drive-realtime-api

大家好,我有这段代码

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.appdata',
    'https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/drive.metadata',
    'https://www.googleapis.com/auth/drive.metadata.readonly',
    'https://www.googleapis.com/auth/drive.photos.readonly',
    'https://www.googleapis.com/auth/drive.readonly'];

const TOKEN_PATH = 'token.json';

// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  // Authorize a client with credentials, then call the Google Drive API.
  authorize(JSON.parse(content), listFiles);
  authorize(JSON.parse(content), downloadFile);
});

function listFiles(auth, drivefiles) {
  const drive = google.drive({version: 'v3', auth});
  drive.files.list({
    pageSize: 10,
    fields: 'nextPageToken, files(id, name)',
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const files = res.data.files;
    if (files.length) {
        //if(files.title contains 'mp4'){
      console.log('Files:');
      files.map((file) => {
        drivefiles = res.data.files;
        console.log(`${file.name} ${file.id}`);
      });
        //}
    } else {
      console.log('No files found.');
    }
  });


}

function downloadFile(auth) {

 const drive = google.drive({version: 'v3', auth});
 var fileId = '1e1IuhYX1XggM9whYr6trI9J-4gZS07kV';
 var dest = fs.createWriteStream('./testds.mp4');
 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);
    }
);
}

im试图下载mp4文件,但为了这样做,我首先需要它的ID。 listfiles函数列出驱动器中的所有文件,所以有没有办法我可以将列表缩小到仅mp4文件,以便我可以获取文件ID并下载它?

就像无论如何,我只能得到我希望我的问题能解决的特定文件的文件ID。任何建议或帮助都将不胜感激!

在此先感谢您的帮助。

0 个答案:

没有答案