Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
chore: delete versioned S3
Browse files Browse the repository at this point in the history
  • Loading branch information
ishibashi_y committed Jul 8, 2019
1 parent 50f9a82 commit b73da7a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ phases:
export BUILD_STATUS="$?"
post_build:
commands:
- npm i -g ts-node && BUCKET_NAME="office-maker-storage-${STAGE_NAME}" ts-node script/deleteVersionedS3.ts
- npx sls remove -s ${STAGE_NAME} && exit ${BUILD_STATUS}

cache:
Expand Down
29 changes: 29 additions & 0 deletions script/deleteVersionedS3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import AWS from 'aws-sdk';

const S3 = new AWS.S3();
const bucketName = process.env.BUCKET_NAME;

const main = async () => {
if (!bucketName) {
throw new Error('BUCKET_NAME is not specified');
}

const versions = (await S3.listObjectVersions({
Bucket: bucketName
}).promise()).Versions;

// Delete one by one
for (const version of versions) {
await S3.deleteObject({
Bucket: bucketName,
Key: version.Key,
VersionId: version.VersionId
}).promise();
}

await S3.deleteBucket({
Bucket: bucketName
}).promise();
};

main();

0 comments on commit b73da7a

Please sign in to comment.