-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (39 loc) · 1.37 KB
/
index.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
47
48
49
'use strict';
const AWS = require('aws-sdk');
const utils = require('../utils');
AWS.config.region = process.env.AWS_REGION || 'eu-west-1';
AWS.config.signatureVersion = 'v4';
exports.handler = async (event, context) => {
const check = utils.checkAuth(event);
if(check.error){
return utils.getResponse(check.error, null, 403);
}
await utils.setCredentials(AWS, process.env.ROLE);
const currentDate = new Date();
const expiresMinutes = 120
const expires = new Date(currentDate.getTime()+(60000*expiresMinutes));
const params = {
Bucket: check.bucket,
Conditions: [
["starts-with", "$key", check.key],
{"bucket": check.bucket},
],
Expiration: expires.toISOString()
};
const s3 = new AWS.S3();
return new Promise((resolve, reject) => {
s3.createPresignedPost(params, function(err, data) {
if (err) {
console.error('Presigning post data encountered an error', err);
} else {
let formdata = {};
formdata.endpoint = data.url;
formdata.key = (check.key ? check.key + "/" : "") + '${filename}';
for(let k in data.fields){
formdata[k] = data.fields[k];
}
resolve(utils.getResponse(null, JSON.stringify(formdata)));
}
});
})
}