-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (47 loc) · 1.39 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const utils = require('../utils');
async function deleteFromS3(bucket, path) {
const listParams = {
Bucket: bucket,
Prefix: path
};
const listedObjects = await s3.listObjectsV2(listParams).promise();
if (listedObjects.Contents.length === 0) return;
const deleteParams = {
Bucket: bucket,
Delete: { Objects: [] }
};
listedObjects.Contents.forEach(({ Key }) => {
deleteParams.Delete.Objects.push({ Key });
});
const deleteResult = await s3.deleteObjects(deleteParams).promise();
if (listedObjects.IsTruncated && deleteResult){
await deleteFromS3(bucket, path);
}
}
exports.handler = async (event, context) => {
const check = utils.checkAuth(event, "delete");
if(check.error){
return utils.getResponse(check.error, null, 403);
}
let keys = null;
if (event.body !== null && event.body !== undefined) {
let body = JSON.parse(event.body);
if (body.keys){
keys = body.keys;
}
}
if(!keys) {
return utils.getResponse("no keys", null, 400);
}
await utils.setCredentials(AWS, process.env.ROLE);
await Promise.all(keys.map(async(key) => {
const newKey = utils.adaptKey(event, key, check.user);
if(newKey){
await deleteFromS3(check.bucket, newKey);
}
}));
return utils.getResponse(null, "{\"message\" : \"done\"}");
};