Skip to content

Instantly share code, notes, and snippets.

@patrickbrandt
Last active May 13, 2017 15:43
Show Gist options
  • Save patrickbrandt/dcbf93c2cba0715c6654bc4b1902297b to your computer and use it in GitHub Desktop.
Save patrickbrandt/dcbf93c2cba0715c6654bc4b1902297b to your computer and use it in GitHub Desktop.
const bb = require('bluebird');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const removeObject = (bucketName, obj) => {
const params = {
Bucket: bucketName,
Key: obj.Key,
};
return s3.deleteObject(params).promise();
};
const emptyBucket = bb.coroutine(function* empty(bucketName) {
const data = yield s3.listObjects({ Bucket: bucketName }).promise();
const tasks = data.Contents.map(obj => removeObject(bucketName, obj));
yield Promise.all(tasks);
});
emptyBucket(process.env.BUCKET_ONE)
.then(() => emptyBucket(process.env.BUCKET_TWO))
.then(() => emptyBucket(process.env.BUCKET_THREE))
.then(() => { /* do something */ })
.catch(err => console.log(err));
// or
Promise.all([
emptyBucket(process.env.BUCKET_ONE),
emptyBucket(process.env.BUCKET_TWO),
emptyBucket(process.env.BUCKET_THREE)])
.then(() => { /* do something */ })
.catch(err => console.log(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment