Skip to content

Instantly share code, notes, and snippets.

@thisjt
Created June 29, 2023 21:09
Show Gist options
  • Save thisjt/56a7d87455eeb35801bdcbf57c7bc320 to your computer and use it in GitHub Desktop.
Save thisjt/56a7d87455eeb35801bdcbf57c7bc320 to your computer and use it in GitHub Desktop.
Function to Download a File from Google Drive
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