# S3


## List Buckets

    var aws = require('plata').connect({'file': 'auth.json'});
    aws.onConnect(function(){
        aws.s3.listBuckets().then(function(buckets){
            console.log('Got buckets: ' + JSON.stringify(buckets, null, 4));
        });
    });


## List Objects

    aws.s3.listKeys('bucketName')
        .then(function(keys){
            console.log('Got objects for bucket.');
            console.log(JSON.stringify(keys, null, 4));
        },
        function(err){
            console.error('Couldnt get objects: ' + e);
        });

## List objects by prefix

    aws.s3.listKeys('bucketName',
        {
            'prefix': 'c',
            'maxKeys': 4
        })
        .then(function(keys){
            console.log('Got objects for bucket.');
            console.log(JSON.stringify(keys, null, 4));
        },
        function(err){
            console.error('Couldnt get objects: ' + e);
        });

## Delte all keys old than 2 days

    var bucketName = 'bucketName',
        toDelete = [],
        threshold = moment().subtract('days', 2)._d.getTime(),
        // Use with a lateral to only run 10 requests at a time.
        pool = Lateral.create(function(complete, item, i){
            aws.s3.deleteObject(bucketName, item).then(function(){
                complete();
            });
        }, 10);
    aws.s3.listKeys(bucketName).then(function(keys){
        keys.forEach(function(key){
            if(new Date(key.lastModified).getTime() < threshold){
                console.log('DELETE ' + key.key + '.  ' + key.lastModified);
                toDelete.push(key.key);
            }
        });
        if(toDelete.length ===  0){
            return console.log('Nothing to delete right now.');
        }
        console.log('Have ' + toDelete.length+ ' keys to delete.');
        pool.add(toDelete, function(){
            console.log('All done.');
        });
    });