Created
June 29, 2023 21:09
-
-
Save thisjt/56a7d87455eeb35801bdcbf57c7bc320 to your computer and use it in GitHub Desktop.
Function to Download a File from Google Drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const keys = require('./auth.json') | |
const fs = require('fs') | |
const {JWT} = require('google-auth-library') | |
const {drive} = require('googleapis/build/src/apis/drive') | |
// DELETE ALL FILES in "apis" folder except "drive" to slim down "googleapis" file size | |
function downloadFileToGdrive() { | |
const auth = new JWT({ | |
email: keys.client_email, | |
key: keys.private_key, | |
scopes: ['https://www.googleapis.com/auth/drive'], | |
}) | |
const service = drive({version: 'v3', auth}) | |
var fileStream = fs.createWriteStream('fileToDownload.zip') | |
try { | |
service.files.get({ | |
fileId: 'FileID', | |
alt: 'media', | |
}, {responseType: 'stream'}).then(res => { | |
res.data.pipe(fileStream) | |
}) | |
} catch (err) { | |
throw err | |
} | |
} | |
downloadFileToGdrive() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment