Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Node.js to get a list of files from Google Drive and save them locally
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
You can’t perform that action at this time.