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

refactor clean.js

This commit is contained in:
Cyrille 2019-05-08 18:02:32 +01:00
parent 7265e5ae2e
commit 5e0aecf8ad

View File

@ -1,13 +1,13 @@
console.log('Emptying the bucket.'); console.log("Emptying the bucket.");
require('dotenv').config(); require("dotenv").config();
var accessKeyId = process.env.AWS_ACCESS_KEY_ID let accessKeyId = process.env.AWS_ACCESS_KEY_ID
secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY , secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY
regionName = process.env.AWS_BUCKET_REGION , regionName = process.env.AWS_BUCKET_REGION
bucketName = process.env.AWS_BUCKET_NAME , bucketName = process.env.AWS_BUCKET_NAME
var AWS = require('aws-sdk'); var AWS = require("aws-sdk");
AWS.config.setPromisesDependency(Promise); AWS.config.setPromisesDependency(Promise);
AWS.config.update({ AWS.config.update({
region: regionName region: regionName
@ -17,13 +17,13 @@ AWS.config.update({
}) })
}); });
const s3 = new AWS.S3({ apiVersion: '2006-03-01' }); const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
s3.listObjects({ Bucket: bucketName }).promise().then((data) => { s3.listObjects({ Bucket: bucketName }).promise().then((data) => {
if (data.Contents.length <= 0) { if (data.Contents.length <= 0) {
console.log('Your bucket is already empty :)'); console.log("Your bucket is already empty :)");
return return;
} }
var objectKeys = []; var objectKeys = [];
@ -31,11 +31,11 @@ s3.listObjects({ Bucket: bucketName }).promise().then((data) => {
for (let i = 0; i < data.Contents.length; i++) { for (let i = 0; i < data.Contents.length; i++) {
objectKeys.push({ objectKeys.push({
Key: data.Contents[i].Key Key: data.Contents[i].Key
}) });
} }
s3.deleteObjects({ Delete: { Objects: objectKeys }, Bucket: bucketName }).promise().then((data) => { s3.deleteObjects({ Delete: { Objects: objectKeys }, Bucket: bucketName }).promise().then((data) => {
console.log('Your bucket was emptied :)'); console.log("Your bucket was emptied :)");
}).catch((err) => { }).catch((err) => {
console.error(err.message); console.error(err.message);
throw err; throw err;