List Objects in a Bucket
List all Objects by Pagination
This SDK method will allow you to get a list of all the objects present in a particular bucket by pagination. The Bucket reference used in the below code snippet is the component instance.
For each call, a limited number of objects will be returned, and the next call will be initiated only if a continuation token is returned.
Parameters Used
Parameter Name | Data Type | Definition |
---|---|---|
maxKey | String | A Mandatory parameter. Will contain the maximum limit of objects that can be listed by pagination. |
nextToken | String | An Mandatory parameter. Will contain the token to get the next set of objects. |
prefix | String | An Optional parameter. To list objects that match the prefix value. |
orderBy | String | An Optional parameter. To list objects either in ascending or descending order. Default Value: asc |
folderListing | String | An Optional parameter. To choose to list either just the root-level objects in the bucket or list all the objects present in all the paths of the bucket. Default Value: false For instance, if you set value as true; the root-level objects alone will be listed. If you set the value as false; all the objects present in all the paths of the bucket will be listed |
In the following SDK method, a maximum value of pagination is set using maxKey. Using prefix, you can list objects that only match the prefix.
The response we get will contain the following properties of the bucket, which will be stored in moreOptions:
- key count: Will contain the value of the number of objects that are being returned
- max keys: The maximum limit of objects that can be returned
- Truncated: Will contain the status to notify if a bucket is truncated or not.
- contents: List of object details
- continuation_token: If you a sent a continuation_token in the request, it will be shown in the response.
- next_continuation_token: If the response was truncated, the value of this key must be passed as continuation_token to the same method for retrieving the next set of objects.
With each iteration, we will list the maxKey number of objects and check if nextToken has been created. Using nextToken we will continue the iteration till all the objects have been listed.
copyasync function listMyPaginatedObjects(maxKeys = null, prefix = null, nextToken = null) { const options = { // Maximum number of keys to return in one call maxKeys, // Token to fetch the next page of objects continuationToken: nextToken, // Order in which objects are listed: 'asc' or 'desc' // orderBy: 'desc', // Whether to list objects in a folder-like structure (true) or flat structure (false) // folderListing: 'true', // Only list objects with this prefix prefix }; // Retrieve a page of objects const objects = await bucket.listPagedObjects(options); console.log("response:", objects.contents); // If more objects are available, recursively fetch the next set if (objects.truncated) { listMyPaginatedObjects(maxKeys, prefix, objects.next_continuation_token); } } // Initial call to list objects with a maximum of 2 keys per page and prefix "sam" await listMyPaginatedObjects(5, "sam");
Example Response
copy{ "prefix": "sam", "key_count": "5", "max_keys": "5", "truncated": "True", "next_continuation_token": "47VrqTzR9ukMF9gr8YcziVVzdRP5GCjq1NfM5fMBpMfvw5qcXFRSueuqCTRUCzNd9dHfquXHi2afDanLH6MbyJo6", "contents": [ { "key_type": "file", "key": "sam1s2ww.mp4", "size": "427160684", "content_type": "video/mp4", "etag": "78c2b173b56cd944e9c79abd601f6073", "last_modified": "May 21, 2024 01:00 PM" }, { "key_type": "file", "key": "samdm.txt", "size": "23", "content_type": "text/plain; charset=utf-8", "etag": "c0122754f465e42eb97b5af174663c29", "last_modified": "May 14, 2024 01:30 PM" }, { "key_type": "file", "key": "samplvbse1.json", "size": "8", "content_type": "application/json", "etag": "499e7dbaee453352a9c17407a676dbda", "last_modified": "May 13, 2024 10:05 AM" }, { "key_type": "file", "key": "samplse1.json", "size": "8", "content_type": "application/json", "etag": "499e7dbaee453352a9c17407a676dbda", "last_modified": "May 13, 2024 09:20 AM" }, { "key_type": "file", "key": "sampjkhdldbed.mp4", "size": "0", "content_type": "video/mp4", "etag": "d41d8cd98f00b204e9800998ecf8427e", "last_modified": "May 12, 2024 10:54 PM" } ] }
List Objects Through Iteration
Using this SDK method, you can list all the objects present in a bucket in a single API call, using iteration technique. Using the maxKey variable, you can access the required number of objects.
copyconst options = { // Maximum number of objects returned per request maxKeys: 5, // Order in which objects are listed: 'asc' or 'desc' // orderBy: 'desc', // Whether to list objects in a folder-like structure (true) or flat structure (false) // folderListing: 'true', // Only list objects that begin with the specified prefix prefix: 's' }; // List iterable files from the bucket const files = bucket.listIterableObjects(options); for await (const file of files) { console.log('file:', file); }
Example Response
copy{ "key_type": "file", "key": "ssdgs.mp4", "size": "3145728", "content_type": "video/mp4", "etag": "9685b8d5b8b719274bac854b897d95ec", "last_modified": "May 21, 2024 03:49 PM" } { "key_type": "file", "key": "Sasss.mp4", "size": "2674", "content_type": "video/mp4", "etag": "24c1122087e9be930ff1e957e83f5224", "last_modified": "May 21, 2024 02:55 PM" } { "key_type": "file", "key": "Samfplessss.mp4", "size": "2674", "content_type": "video/mp4", "etag": "24c1122087e9be930ff1e957e83f5224", "last_modified": "May 21, 2024 02:52 PM" } { "key_type": "file", "key": "demo.mp4", "size": "3400", "content_type": "video/mp4", "etag": "24e957e83f5224c1122087e9be930ff1", "last_modified": "May 21, 2024 02:52 PM" } { "key_type": "file", "key": "performance.mp4", "size": "1454", "content_type": "video/mp4", "etag": "087e9be930ff124c1122e957e83f5224", "last_modified": "May 21, 2024 02:52 PM" }
Last Updated 2025-07-28 16:56:33 +0530 +0530
Yes
No
Send your feedback to us