Created
June 1, 2019 12:29
-
-
Save BretCameron/805cb18abc2b116de982bb6f130e055d to your computer and use it in GitHub Desktop.
Use Node.js to get a list of files from Google Drive and save them locally
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 { google } = require('googleapis'); | |
const fs = require('fs'); | |
const credentials = require('./credentials.json'); | |
const scopes = [ | |
'https://www.googleapis.com/auth/drive', | |
'https://www.googleapis.com/auth/spreadsheets', | |
]; | |
const auth = new google.auth.JWT( | |
credentials.client_email, null, | |
credentials.private_key, scopes | |
); | |
const drive = google.drive({ version: 'v3', auth }); | |
(async function () { | |
let res = await new Promise((resolve, reject) => { | |
drive.files.list({ | |
pageSize: 5, | |
fields: 'files(name, webViewLink)', | |
orderBy: 'createdTime desc' | |
}, function (err, res) { | |
if (err) { | |
reject(err); | |
} | |
resolve(res); | |
}); | |
}); | |
let data = 'Name,URL\n'; | |
res.data.files.map(entry => { | |
const { name, webViewLink } = entry; | |
data += `${name},${webViewLink}\n`; | |
}); | |
fs.writeFile('data.csv', data, (err) => { | |
if (err) throw err; | |
console.log('The file has been saved!'); | |
}); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment