├── .npmignore ├── .gitignore ├── Makefile ├── package.json ├── Readme.md ├── lib └── index.js └── test └── test.js /.npmignore: -------------------------------------------------------------------------------- 1 | /test/ 2 | /.npmignore -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /test/auth.json 2 | /node_modules -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | npm test 4 | 5 | 6 | .PHONY: test -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "s3-lister", 3 | "version": "0.1.0", 4 | "description": "Stream the file descriptions from all keys in an s3 bucket", 5 | "main": "lib/index.js", 6 | "repository": "https://github.com/segmentio/s3-lister.git", 7 | "license": "MIT", 8 | "scripts": { 9 | "test": "mocha" 10 | }, 11 | "dependencies": { 12 | "readable-stream": "~1.0.2" 13 | }, 14 | "peerDependencies": { 15 | "knox": ">=0.5.0" 16 | }, 17 | "devDependencies": { 18 | "knox": "~0.8.3", 19 | "mocha": "*" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | S3-Lister 2 | ========= 3 | 4 | A simple library to stream all the files which are the contents of an s3 folder. 5 | 6 | 7 | ## Usage 8 | 9 | ```javascript 10 | // Print all the files for a given prefix. 11 | 12 | var client = knox.createClient({ 13 | key : '', 14 | secret : '', 15 | bucket : 'segmentio' 16 | }); 17 | 18 | var lister = new S3Lister(client, {prefix : 'logs/api/a0z4'}); 19 | 20 | lister 21 | .on('data', function (data) { console.log(data.Key); }) 22 | .on('error', function (err) { console.log('Error!', err); }) 23 | .on('end', function () { console.log('Done!'); }); 24 | ``` 25 | 26 | 27 | ### new S3Lister(s3, options) 28 | 29 | * s3 - a knox client 30 | * options 31 | 32 | In addition to the standard stream options, you can also pass in specific options: 33 | 34 | * start - string to start with 35 | * prefix - the prefix to list under 36 | * start - s3 will return every key alphabetically after this string 37 | * delimiter - the character you use to group keys 38 | * maxResults - maximum amount of keys to list in total 39 | * maxKeys - maximum amount of keys in a batch, (limited to 1000) 40 | 41 | ## License 42 | 43 | (The MIT License) 44 | 45 | Copyright (c) 2013 Segment.io 46 | 47 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 48 | 49 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 50 | 51 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 52 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 2 | var stream = require('readable-stream') 3 | , util = require('util'); 4 | 5 | 6 | module.exports = S3Lister; 7 | 8 | 9 | /** 10 | * Create a new S3Lister 11 | * @param {Object} s3 a knox-like client 12 | * @param {Object} options 13 | * @field {Boolean} start key to start with 14 | * @field {Boolean} maxResults maximum amount of objects to list 15 | */ 16 | function S3Lister (s3, options) { 17 | options || (options = {}); 18 | options.objectMode = true; 19 | 20 | stream.Readable.call(this, options); 21 | 22 | this.s3 = s3; 23 | this.marker = options.start; 24 | this.options = options; 25 | this.connecting = false; 26 | this.resultCount = 0; 27 | this.maxResults = options.maxResults; 28 | } 29 | util.inherits(S3Lister, stream.Readable); 30 | 31 | 32 | /** 33 | * Readable stream method 34 | */ 35 | S3Lister.prototype._read = function () { 36 | if (this.connecting || this.ended) return; 37 | 38 | var options = { 39 | prefix : this.options.prefix, 40 | marker : this.marker, 41 | delimiter : this.options.delimiter, 42 | 'max-keys' : this.options.maxKeys 43 | }; 44 | 45 | this._list(options); 46 | }; 47 | 48 | 49 | /** 50 | * Request S3 for the list of files matching the prefix. 51 | * @param {Object} options 52 | * @field {String} prefix - prefix to match on (optional) 53 | * @field {String} marker - the last matched file (optional) 54 | * @field {String} delimiter - delimiter to split files (optional) 55 | * @field {Number} maxKeys - max number of keys to return (optional, 1000) 56 | */ 57 | S3Lister.prototype._list = function (options) { 58 | var self = this; 59 | this.connecting = true; 60 | 61 | this.s3.list(options, function (err, data) { 62 | self.connecting = false; 63 | if (err) return self.emit('error', err); 64 | 65 | var files = data.Contents; 66 | 67 | // if there's still more data, set the start as the last file 68 | if (data.IsTruncated) self.marker = files[files.length - 1].Key; 69 | else self.ended = true; 70 | 71 | files.forEach(function (file) { 72 | self.resultCount++; 73 | if (self.maxResults && self.resultCount > self.maxResults) { 74 | self.ended = true; 75 | return; 76 | } 77 | self.push(file); 78 | }); 79 | if (self.ended) self.push(null); 80 | }); 81 | }; 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 2 | var assert = require('assert') 3 | , auth = require('./auth.json') 4 | , knox = require('knox') 5 | , S3Lister = require('../'); 6 | 7 | 8 | var client = knox.createClient(auth); 9 | 10 | 11 | describe('S3Lister', function () { 12 | 13 | var files = 10 14 | , folder = '_s3-list-test'; 15 | 16 | function fileOperation (fn, done) { 17 | var completed = 0; 18 | 19 | function cb (err, res) { 20 | if (err) return done(err); 21 | completed += 1; 22 | if (completed >= files) done(); 23 | } 24 | 25 | for (var i = 0; i < files; i++) { 26 | var filename = folder + '/' + i + '.txt'; 27 | fn(filename, cb); 28 | } 29 | } 30 | 31 | before(function (done) { 32 | function upload(filename, cb) { 33 | client.putBuffer(new Buffer(filename), filename, cb); 34 | } 35 | 36 | fileOperation(upload, done); 37 | }); 38 | 39 | 40 | it ('should list all the files', function (done) { 41 | var stream = new S3Lister(client, { prefix : folder }) 42 | , filesSeen = 0; 43 | 44 | stream 45 | .on('data', function (file) { filesSeen += 1; }) 46 | .on('error', function (err) { done(err); }) 47 | .on('end', function () { 48 | assert.equal(filesSeen, files); 49 | done(); 50 | }); 51 | }); 52 | 53 | it ('should list n files when options.maxResults is set to n', function (done) { 54 | var maxResults = 5 55 | , stream = new S3Lister(client, { maxResults : maxResults }) 56 | , filesSeen = 0; 57 | 58 | stream 59 | .on('data', function (file) { filesSeen += 1; }) 60 | .on('error', function (err) { done(err); }) 61 | .on('end', function () { 62 | assert.equal(maxResults, filesSeen); 63 | done(); 64 | }); 65 | }); 66 | 67 | it('should list all the files if maxKeys < number of files', function (done) { 68 | var stream = new S3Lister(client, { 69 | maxKeys : 6, 70 | prefix : folder 71 | }); 72 | 73 | var filesSeen = 0; 74 | 75 | stream 76 | .on('data', function (file) { filesSeen += 1; }) 77 | .on('error', function (err) { done(err); }) 78 | .on('end', function () { 79 | assert.equal(filesSeen, files); 80 | done(); 81 | }); 82 | }); 83 | 84 | 85 | it('should respect the file start', function (done) { 86 | var start = 4; 87 | 88 | var stream = new S3Lister(client, { 89 | maxKeys : 6, 90 | prefix : folder, 91 | start : folder + '/' + start + '.txt' 92 | }); 93 | 94 | var filesSeen = 0; 95 | 96 | stream 97 | .on('data', function (file) { filesSeen += 1; }) 98 | .on('error', function (err) { done(err); }) 99 | .on('end', function () { 100 | assert.equal(filesSeen, files - start - 1); 101 | done(); 102 | }); 103 | }); 104 | 105 | 106 | after(function (done) { 107 | function remove(filename, cb) { 108 | client.deleteFile(filename, cb); 109 | } 110 | 111 | fileOperation(remove, done); 112 | }); 113 | }); 114 | --------------------------------------------------------------------------------