-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfiles.js
46 lines (41 loc) · 1.03 KB
/
files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
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' })
// });
}