-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean.js
46 lines (37 loc) · 1.21 KB
/
clean.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
console.log("Emptying the bucket.");
require("dotenv").config();
let accessKeyId = process.env.AWS_ACCESS_KEY_ID
, secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY
, regionName = process.env.AWS_BUCKET_REGION
, bucketName = process.env.AWS_BUCKET_NAME;
var AWS = require("aws-sdk");
AWS.config.setPromisesDependency(Promise);
AWS.config.update({
region: regionName
, credentials: new AWS.Credentials({
accessKeyId: accessKeyId
, secretAccessKey: secretAccessKey
})
});
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
s3.listObjects({ Bucket: bucketName }).promise().then((data) => {
if (data.Contents.length <= 0) {
console.log("Your bucket is already empty :)");
return;
}
var objectKeys = [];
for (let i = 0; i < data.Contents.length; i++) {
objectKeys.push({
Key: data.Contents[parseInt(i)].Key
});
}
s3.deleteObjects({ Delete: { Objects: objectKeys }, Bucket: bucketName }).promise().then((data) => {
console.log("Your bucket was emptied :)");
}).catch((err) => {
console.error(err.message);
throw err;
});
}).catch((err) => {
console.error(err.message);
throw err;
});