2
0
mirror of https://github.com/cderche/greenlock-challenge-s3 synced 2025-05-10 15:36:33 +00:00

Created clean.js

This commit is contained in:
Cyrille 2019-05-08 17:27:54 +01:00
parent a33684dca8
commit 16244df438
2 changed files with 47 additions and 0 deletions

46
clean.js Normal file
View File

@ -0,0 +1,46 @@
console.log('Emptying the bucket.');
require('dotenv').config();
var 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[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;
});

View File

@ -4,6 +4,7 @@
"description": "S3 backed challenge strategy for greenlock-express.js (and greenlock.js)",
"main": "index.js",
"scripts": {
"clean": "node ./clean.js",
"test": "node ./test.js"
},
"repository": {