AWS S3 object listing

You can use the Prefix in s3 API params. I am adding an example that i used in a project:

listBucketContent: ({ Bucket, Folder }) => new Promise((resolve, reject) => {
    const params = { Bucket, Prefix: `${Folder}/` };
    s3.listObjects(params, (err, objects) => {
        if (err) {
            reject(ERROR({ message: 'Error finding the bucket content', error: err }));
        } else {
            resolve(SUCCESS_DATA(objects));
        }
    });
})

Here Bucket is the name of the bucket that contains a folder and Folder is the name of the folder that you want to list files in.


It's working fine now using this code :

var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: 'mykey', secretAccessKey: 'mysecret', region: 'myregion'});
var s3 = new AWS.S3();

var params = { 
 Bucket: 'mystore.in',
 Delimiter: '/',
 Prefix: 's/5469b2f5b4292d22522e84e0/ms.files/'
}

s3.listObjects(params, function (err, data) {
 if(err)throw err;
 console.log(data);
});

Folders are illusory, but S3 does provide a mechanism to emulate their existence.

If you set Delimiter to / then each tier of responses will also return a CommonPrefixes array of the next tier of "folders," which you'll append to the prefix from this request, to retrieve the next tier.

If your Prefix is a "folder," append a trailing slash. Otherwise, you'll make an unnecessary request, because the first request will return one common prefix. E.g., folder "foo" will return one common prefix "foo/".


I put up a little module which lists contents of a "folder" you give it:

s3ls({bucket: 'my-bucket-name'}).ls('/', console.log);

will print something like this:

{ files: [ 'funny-cat-gifs-001.gif' ],
  folders: [ 'folder/', 'folder2/' ] }

And that

s3ls({bucket: 'my-bucket-name'}).ls('/folder', console.log);

will print

{ files: [ 'folder/cv.docx' ],
  folders: [ 'folder/sub-folder/' ] }

UPD: The latest version supports async/await Promise interface:

const { files, folders } = await lister.ls("/my-folder/subfolder/");

And here is the s3ls.js:

var _ = require('lodash');
var S3 = require('aws-sdk').S3;

module.exports = function (options) {
  var bucket = options.bucket;
  var s3 = new S3({apiVersion: '2006-03-01'});

  return {
    ls: function ls(path, callback) {
      var prefix = _.trimStart(_.trimEnd(path, '/') + '/', '/');    
      var result = { files: [], folders: [] };

      function s3ListCallback(error, data) {
        if (error) return callback(error);

        result.files = result.files.concat(_.map(data.Contents, 'Key'));
        result.folders = result.folders.concat(_.map(data.CommonPrefixes, 'Prefix'));

        if (data.IsTruncated) {
          s3.listObjectsV2({
            Bucket: bucket,
            MaxKeys: 2147483647, // Maximum allowed by S3 API
            Delimiter: '/',
            Prefix: prefix,
            ContinuationToken: data.NextContinuationToken
          }, s3ListCallback)
        } else {
          callback(null, result);
        }
      }

      s3.listObjectsV2({
        Bucket: bucket,
        MaxKeys: 2147483647, // Maximum allowed by S3 API
        Delimiter: '/',
        Prefix: prefix,
        StartAfter: prefix // removes the folder name from the file listing
      }, s3ListCallback)
    }
  };
};