Created
August 12, 2019 18:45
-
-
Save mornir/4aa485bbdb82a2c7056e0840f1a2b7b4 to your computer and use it in GitHub Desktop.
Netlify Cloud Function List 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 {google} = require('googleapis'); | |
const path = require('path'); | |
const fs = require('fs'); | |
function getCredentials() { | |
const filePath = path.join(__dirname, 'keys.json'); | |
if (fs.existsSync(filePath)) { | |
return require(filePath); | |
} | |
if (process.env.CREDENTIALS) { | |
return JSON.parse(process.env.CREDENTIALS); | |
} | |
throw new Error('Unable to load credentials'); | |
} | |
async function getFiles () { | |
const credentials = getCredentials(); | |
const client = await google.auth.getClient({ | |
credentials, | |
scopes: 'https://www.googleapis.com/auth/drive.readonly' | |
}); | |
const drive = google.drive({ | |
version: 'v2', | |
auth: client | |
}); | |
return drive.files.list(); | |
} | |
exports.handler = function(event, context, callback) { | |
console.log('DATE! ' + (new Date())); | |
getFiles().then(res => { | |
callback(null, { | |
statusCode: 200, | |
body: JSON.stringify(res.data) | |
}); | |
}).catch(e => { | |
callback(e); | |
}); | |
// callback(null, { | |
// statusCode: 200, | |
// body: JSON.stringify({ foo: 'bar' }) | |
// }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment