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.
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.
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.
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.
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
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") }) } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With