Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET list of objects located under a specific S3 folder

Tags:

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.

like image 571
appy Avatar asked May 16 '18 09:05

appy


People also ask

How do I view contents of a S3 bucket?

To open the overview pane for an objectSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object. In the Objects list, choose the name of the object for which you want an overview.

Where are the objects stored in S3?

All objects are stored in S3 buckets and can be organized with shared names called prefixes. You can also append up to 10 key-value pairs called S3 object tags to each object, which can be created, updated, and deleted throughout an object's lifecycle.

Are folders objects in S3?

In Amazon S3, folders are used to group objects and organize files. Unlike a traditional file system, Amazon S3 doesn't use hierarchy to organize its objects and files. Amazon S3 console supports the folder concept only as a means of grouping (and displaying) objects.


2 Answers

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

like image 149
Abhyudit Jain Avatar answered Sep 20 '22 16:09

Abhyudit Jain


You forget to mention folder into s3 bucket, anyways this code works for me

var params = { Bucket:Bucket_Name, Delimiter: '/', Prefix: 'foldername/' };  s3Bucket.listObjects(params, function(err, data) {             if (err) {                 return 'There was an error viewing your album: ' + err.message             }else{                 console.log(data.Contents,"<<<all content");                  data.Contents.forEach(function(obj,index){                     console.log(obj.Key,"<<<file path")                 })             }         }) 
like image 35
Amit Shakya Avatar answered Sep 21 '22 16:09

Amit Shakya