2
0
mirror of https://github.com/cderche/greenlock-storage-s3 synced 2025-02-22 14:18:05 +00:00
gl-store-s3.js/clean.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-05-09 00:01:23 +01:00
console.log("Emptying the bucket.");
2019-05-08 12:19:12 +01:00
2019-05-09 00:01:23 +01:00
require("dotenv").config();
2019-05-08 12:19:12 +01:00
2019-05-09 00:01:23 +01:00
let accessKeyId = process.env.AWS_ACCESS_KEY_ID
, secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY
, regionName = process.env.AWS_BUCKET_REGION
2019-05-09 00:12:15 +01:00
, bucketName = process.env.AWS_BUCKET_NAME;
2019-05-08 12:19:12 +01:00
2019-05-09 00:01:23 +01:00
var AWS = require("aws-sdk");
2019-05-08 12:19:12 +01:00
AWS.config.setPromisesDependency(Promise);
AWS.config.update({
region: regionName
, credentials: new AWS.Credentials({
2019-05-09 00:12:15 +01:00
accessKeyId
, secretAccessKey
2019-05-08 12:19:12 +01:00
})
});
2019-05-09 00:01:23 +01:00
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
2019-05-08 12:19:12 +01:00
2019-05-08 14:05:05 +01:00
s3.listObjects({ Bucket: bucketName }).promise().then((data) => {
2019-05-08 12:19:12 +01:00
if (data.Contents.length <= 0) {
2019-05-09 00:01:23 +01:00
console.log("Your bucket is already empty :)");
return;
2019-05-08 12:19:12 +01:00
}
var objectKeys = [];
2019-05-08 14:05:05 +01:00
for (let i = 0; i < data.Contents.length; i++) {
2019-05-08 12:19:12 +01:00
objectKeys.push({
2019-05-09 08:30:54 +01:00
Key: data.Contents[parseInt(i)].Key
2019-05-09 00:01:23 +01:00
});
2019-05-08 14:05:05 +01:00
}
s3.deleteObjects({ Delete: { Objects: objectKeys }, Bucket: bucketName }).promise().then((data) => {
2019-05-09 00:01:23 +01:00
console.log("Your bucket was emptied :)");
2019-05-08 14:05:05 +01:00
}).catch((err) => {
2019-05-08 12:19:12 +01:00
console.error(err.message);
throw err;
});
2019-05-08 14:05:05 +01:00
}).catch((err) => {
2019-05-08 12:19:12 +01:00
console.error(err.message);
throw err;
});