const async = require('async');
const AWS = require('aws-sdk');
const http = require('http');

// configurable params
const BUCKET = 'foo';
const LISTING_LIMIT = 300;
const ACCESSKEY = 'accessKey1';
const SECRETKEY = 'verySecretKey1';
const ENDPOINT = 'http://127.0.0.1:8000';
const QUIET_MODE = true;

AWS.config.update({
    accessKeyId: ACCESSKEY,
    secretAccessKey: SECRETKEY,
    region: "us-west-1",
    sslEnabled: false,
    endpoint: ENDPOINT,
    s3ForcePathStyle: true,
    apiVersions: { s3: '2006-03-01' },
    signatureVersion: 'v4',
    signatureCache: false,
});

const s3 = new AWS.S3({
    httpOptions: {
        maxRetries: 0,
        timeout: 0,
        agent: new http.Agent({ keepAlive: true }),
    },
});

// list object versions
function _listObjectVersions(VersionIdMarker, KeyMarker, cb) {
    s3.listObjectVersions({ Bucket: BUCKET, MaxKeys: LISTING_LIMIT, VersionIdMarker, KeyMarker }, cb);
}

// return object with key and version_id
function _getKeys(keys) {
    return keys.map((v) => ({
        Key: v.Key,
        VersionId: v.VersionId,
    }));
}

function rabbitHole(cb) {
    let VersionIdMarker = null;
    let KeyMarker = null;
    async.doWhilst(
        done => _listObjectVersions(VersionIdMarker, KeyMarker, (err, data) => {
            if (err) {
                return done(err);
            }
            console.log(data);
            VersionIdMarker = data.NextVersionIdMarker;
            KeyMarker = data.NextKeyMarker;
        }),
        () => {
            if (VersionIdMarker ||  KeyMarker) {
                return true;
            }
            return false;
        },
        cb
    );
}

rabbitHole((err, res) => {
    if (err) {
        return console.log('error occured deleting objects', err);
    }
    return console.log('completed deletion');
});

@@ -155,17 +155,19 @@
 LIMIT = 
-3
+100
 00;%0Acons
@@ -185,18 +185,8 @@
  = '
-accessKey1
 ';%0Ac
@@ -207,22 +207,8 @@
  = '
-verySecretKey1
 ';%0Ac
@@ -267,19 +267,20 @@
 _MODE = 
-tru
+fals
 e;%0A%0AAWS.
@@ -358,16 +358,19 @@
 KEY,%0A   
+ //
  region:
@@ -378,10 +378,10 @@
 %22us-
-w
 e
+a
 st-1
@@ -1061,16 +1061,108 @@
 %7D));%0A%7D%0A%0A
+function logger (msg, val) %7B%0A    if (!QUIET_MODE) %7B%0A        console.log(msg, val);%0A    %7D%0A%7D%0A%0A
 function
@@ -1353,32 +1353,70 @@
      if (err) %7B%0A
+                console.log('error');%0A
                 
@@ -1463,125 +1463,354 @@
     
-console.log(data);%0A            VersionIdMarker = data.NextVersionIdMarker;%0A            KeyMarker = data.NextKeyMarker
+// data.Versions.forEach(d =%3E %7B%0A            //     // logger('Key', d.Key, 'VersionId', d.VersionId);%0A            // %7D);%0A            VersionIdMarker = data.NextVersionIdMarker;%0A            KeyMarker = data.NextKeyMarker;%0A            logger('VersionIdMarker', VersionIdMarker);%0A            logger('KeyMarker', KeyMarker);%0A            return done()
 ;%0A  
@@ -1851,16 +1851,17 @@
     if (
+!
 VersionI
@@ -1872,16 +1872,17 @@
 ker %7C%7C  
+!
 KeyMarke
@@ -1909,19 +1909,20 @@
  return 
-tru
+fals
 e;%0A     
@@ -1941,36 +1941,35 @@
          return 
-fals
+tru
 e;%0A        %7D,%0A  
@@ -1974,18 +1974,123 @@
         
-cb
+(err, res) =%3E %7B%0A            console.log('err', err, 'res', res);%0A            return cb(err, res);%0A        %7D
 %0A    );%0A
@@ -2115,24 +2115,65 @@
 , res) =%3E %7B%0A
+    console.log('err', err, 'res', res);%0A
     if (err)
@@ -2217,20 +2217,19 @@
 occured 
-dele
+lis
 ting obj
@@ -2285,16 +2285,15 @@
 ted 
-deletion
+listing
 ');%0A

update rabbithole script