Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matthewdfuller
Created March 20, 2015 19:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save matthewdfuller/abcc38d23e2c73b1ee94 to your computer and use it in GitHub Desktop.
Save matthewdfuller/abcc38d23e2c73b1ee94 to your computer and use it in GitHub Desktop.
List and download all files in a given S3 bucket
/* 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');
}
});
});
@shikharcoursera
Copy link

this code doesn't download the files can you tell how to download the file

@alexander-morris
Copy link

@shikharcoursera - just add something with fs.writeFile in line ~45

@alexander-morris
Copy link

@matthewdfuller thanks for sharing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment