Created
June 6, 2016 03:11
-
-
Save Duder-onomy/e5f5829bcf74c6a8b8ecf96e403d1fa5 to your computer and use it in GitHub Desktop.
Get Full Contents of Amazon s3 Bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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