Skip to content

Instantly share code, notes, and snippets.

@abhisheksharma14
Created November 8, 2019 13:43
Show Gist options
  • Save abhisheksharma14/02c989e991eacb02b3af2ecb837469e1 to your computer and use it in GitHub Desktop.
Save abhisheksharma14/02c989e991eacb02b3af2ecb837469e1 to your computer and use it in GitHub Desktop.
Script to delete/restore objects from S3
var AwsS3 = require ('aws-sdk/clients/s3');
let cred = {
test: {
"accessKeyId": "<test-key>",
"secretAccessKey": "<test-secret>",
"region": '<region>',
},
live: {
"accessKeyId": "<live-key>",
"secretAccessKey": "<live-secret>",
"region": "<region>",
}
};
const s3 = new AwsS3 (cred.test);
let config = {
Bucket: "<bucket-name>"
};
async function listDirectories (prefix, marker) {
return new Promise ((resolve, reject) => {
const s3params = {
...config,
MaxKeys: 1000
};
if(prefix) {
s3params['Prefix'] = prefix;
}
if(marker) {
s3params['KeyMarker'] = marker;
}
s3.listObjectVersions (s3params, (err, data) => {
if (err) {
reject (err);
}
resolve (data);
});
});
}
async function deleteDirectory(Key, VersionId, action="restore"){
return new Promise ((resolve, reject) => {
const s3params = { ...config, Key, VersionId};
if (action == "delete") delete s3params.Prefix;
s3.deleteObject (s3params, (err, data) => {
if (err) {
reject (err);
}
resolve (data);
});
});
}
async function main(prefix, action="restore") {
let deleteMarkers = [];
let marker = "";
let count=0;
while (true) {
count++;
let listRes = await listDirectories(prefix, marker)
.catch(err => console.log(err, `Unable to list Directories at ${count}th loop, Skipping...`));
if (!listRes) continue;
marker = decodeURIComponent(listRes.NextKeyMarker);
if (action == "delete")
deleteMarkers.push(...listRes.Versions);
else if (action == "restore")
deleteMarkers.push(...listRes.DeleteMarkers);
console.log(`Loop Count :: ${count} , Next Marker :: ${ marker}`);
if (!listRes.NextKeyMarker) break;
}
for (let i = 0 ; i < deleteMarkers.length ; i++) {
await deleteDirectory(deleteMarkers[i].Key, deleteMarkers[i].VersionId, action)
.then(data => console.log("Deleted Object :: ", deleteMarkers[i].Key))
.catch(err => console.error("Error deleting Object :: ", err.message , " :: " , deleteMarkers[i].Key));
}
}
main( "<prefix>", "restore");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment