Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js & Amazon S3: How to iterate through all files in a bucket?

Is there any Amazon S3 client library for Node.js that allows listing of all files in S3 bucket?

The most known aws2js and knox don't seem to have this functionality.

like image 633
nab Avatar asked Feb 24 '12 20:02

nab


People also ask

What is NodeJS used for?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.

Is NodeJS frontend or backend?

A common misconception among developers is that Node. js is a backend framework and is only used for building servers. This isn't true: Node. js can be used both on the frontend and the backend.

Is NodeJS better than Python?

Node is better for web applications and website development whereas Python is best suitable for back-end applications, numerical computations and machine learning. Nodejs utilize JavaScript interpreter whereas Python uses CPython as an interpreter.

Is NodeJS a programming language?

Is Node JS a Language? No. Node JS is not a programming language, but it allows developers to use JavaScript, which is a programming language that allows users to build web applications. This tool is mostly used by programmers who use JavaScript to write Server-Side scripts.


1 Answers

Using the official aws-sdk:

var allKeys = []; function listAllKeys(marker, cb) {   s3.listObjects({Bucket: s3bucket, Marker: marker}, function(err, data){     allKeys.push(data.Contents);      if(data.IsTruncated)       listAllKeys(data.NextMarker, cb);     else       cb();   }); } 

see s3.listObjects

Edit 2017: Same basic idea, but listObjectsV2( ... ) is now recommended and uses a ContinuationToken (see s3.listObjectsV2):

var allKeys = []; function listAllKeys(token, cb) {   var opts = { Bucket: s3bucket };   if(token) opts.ContinuationToken = token;    s3.listObjectsV2(opts, function(err, data){     allKeys = allKeys.concat(data.Contents);      if(data.IsTruncated)       listAllKeys(data.NextContinuationToken, cb);     else       cb();   }); } 
like image 62
Meekohi Avatar answered Sep 19 '22 22:09

Meekohi