Skip to content

Instantly share code, notes, and snippets.

@Duder-onomy
Created June 6, 2016 03:11
Show Gist options
  • Save Duder-onomy/e5f5829bcf74c6a8b8ecf96e403d1fa5 to your computer and use it in GitHub Desktop.
Save Duder-onomy/e5f5829bcf74c6a8b8ecf96e403d1fa5 to your computer and use it in GitHub Desktop.
Get Full Contents of Amazon s3 Bucket
'use strict';
var applicationConfigs = require('expressively').configs,
AWS = require('aws-sdk'),
BB = require('bluebird'),
path = require('path'),
logger = require('../lib/logger'),
s3Instance;
AWS.config.update({
accessKeyId: applicationConfigs.grasshopper.assets.engines.amazon.accessKeyId,
secretAccessKey: applicationConfigs.grasshopper.assets.engines.amazon.secretAccessKey,
region: applicationConfigs.grasshopper.assets.engines.amazon.region
});
s3Instance = new AWS.S3();
module.exports = function getFullContentsOfAmazonBucket(bucketId) {
return _getBatchRecursive(bucketId, [])
.then(function(collectedObjects) {
return collectedObjects
.filter(function(amazonImageObject) {
return !!path.extname(amazonImageObject.Key);
})
.map(function(amazonImageObject) {
return applicationConfigs.assetsPrefix + amazonImageObject.Key;
});
})
.catch(function(err) {
logger.error(`COULD NOT RETRIEVE AMAZON BUCKET: ${bucketId}`);
logger.error(err);
});
};
function _getBatchRecursive(bucketId, collectedObjects, marker) {
var s3Options = {
Bucket: applicationConfigs.grasshopper.assets.engines.amazon.bucket,
Prefix: bucketId
};
if(marker) {
s3Options.Marker = marker;
}
return new BB(function(resolve, reject) {
s3Instance.listObjects(s3Options,
function(err, data) {
if(err) {
reject(err);
} else {
collectedObjects = collectedObjects.concat(data.Contents);
if (data.IsTruncated) {
return _getBatchRecursive(bucketId, collectedObjects, data.Contents[data.Contents.length -1].Key)
.then(resolve);
} else {
resolve(collectedObjects);
}
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment