Created
March 20, 2015 19:36
-
-
Save matthewdfuller/abcc38d23e2c73b1ee94 to your computer and use it in GitHub Desktop.
List and download all files in a given 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
/* Installation: | |
* npm install aws-sdk | |
* npm install async | |
* node awsDownloadFilesInBucket.js | |
*/ | |
// SETTINGS | |
var AWS_KEY = ''; | |
var AWS_SECRET = ''; | |
var BUCKET = ''; | |
var PREFIX = ''; | |
var async = require('async'); | |
var fs = require('fs'); | |
var AWS = require('aws-sdk'); | |
AWS.config.update({accessKeyId: AWS_KEY, secretAccessKey: AWS_SECRET}); | |
var s3 = new AWS.S3(); | |
var params = { | |
Bucket: BUCKET, | |
Prefix: PREFIX | |
} | |
s3.listObjects(params, function(err, data){ | |
if (err) return console.log(err); | |
async.eachSeries(data.Contents, function(fileObj, callback){ | |
var key = fileObj.Key; | |
console.log('Downloading: ' + key); | |
var fileParams = { | |
Bucket: BUCKET, | |
Key: key | |
} | |
s3.getObject(fileParams, function(err, fileContents){ | |
if (err) { | |
callback(err); | |
} else { | |
// Read the file | |
var contents = fileContents.Body.toString(); | |
// Do something with file | |
callback(); | |
} | |
}); | |
}, function(err) { | |
if (err) { | |
console.log('Failed: ' + err); | |
} else { | |
console.log('Finished'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@shikharcoursera - just add something with fs.writeFile in line ~45