GET list of objects located under a specific S3 folder

appy picture appy · May 16, 2018 · Viewed 22.7k times · Source

I am trying to GET a list of objects located under a specific folder in an S3 bucket using a query-string which takes the foldername as the parameter and list all objects which match that specific folder using Node JS aws-sdk

For example: http://localhost:3000/listobjects?foldername=xxx

Please suggest how to implement this functionality.

Answer

Abhyudit Jain picture Abhyudit Jain · May 16, 2018

You can specify the prefix while calling the getObject or listObjectsV2 in aws-sdk

var params = {
  Bucket: 'STRING_VALUE', /* required */
  Prefix: 'STRING_VALUE'  // Can be your folder name
};
s3.listObjectsV2(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

By the way, S3 doesn't have folders. It is just a prefix. It shows you the folder structure to make it easy for you too navigate and see files.

Source: AWS SDK Javascript