List Objects in a Bucket
List all Objects by Pagination
This SDK method will allow you to get 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 |
---|---|---|
max_keys | String | A Mandatory parameter. Will contain the maximum limit of objects that can be listed by pagination. |
continuation_token | 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. |
order_by | String | An Optional parameter. To list objects either in ascending or descending order. Default Value: asc |
folder_listing | 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 max_keys. 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_key: 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
- next_token: If the response was truncated, the value of this key must be passed as next_token to the same method for retrieving the next set of objects.
With each iteration, we will list the max_keys number of objects and check if next_token has been created. Using next_token we will continue the iteration till all the objects have been listed.
copy# Define a recursive function to list objects from the bucket using pagination def list_my_paged_objects(max_keys=None, prefix=None, next_token=None): # Fetch a paged list of objects from the bucket with specified options data = bucket.list_paged_objects( max_keys, # Maximum number of objects to retrieve in this call prefix, # Filter objects that start with this prefix next_token, # Continuation token to fetch the next page of results folder_listing=True, # List objects in a folder-like structure order_by='desc' # Sort objects in descending order (most recent first) ) # Print the list of retrieved objects print(data['contents']) # Check if more objects are available (pagination is not yet complete) if data['truncated']: # Recursively call the function to fetch the next page of objects list_my_paged_objects(max_keys, prefix, data['next_continuation_token']) #Start listing objects with a page size of 2 list_my_paged_objects(2, '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 get all the objects present in a bucket in a single API call, using iteration technique. The Bucket reference used in the below code snippet is the component instance.
copy# List objects from the bucket using iterable pagination with specified options objects = bucket.list_iterable_objects( max_keys=5, # Maximum number of objects to retrieve per batch (default is 1000) prefix='sam', # Filter objects that start with this prefix folder_listing=True, # List objects in a folder-like structure (default is False) order_by='desc' # Sort objects in descending order (default is 'asc') ) #Iterate over and print each object key for key in objects: print(key)
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