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 |
---|---|---|
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. |
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.
Ensure the following packages are imported:
copyimport com.zc.component.stratus.ZCBucket; import com.zc.component.stratus.ZCStratus; import com.zc.component.stratus.beans.ZCPagedObjectResponse; import com.zc.component.stratus.ZCObject;
copyString nextToken = null; int maxKey = 5; String prefix="sam"; do { ZCPagedObjectResponse objects = bucket.listPagedObjects(maxKey, nextToken, prefix); // return the maxKey number of objects in the bucket System.out.println("object count: "+objects.getKeyCount()); System.out.println("max key: "+objects.getMaxKey()); System.out.println("truncated status: "+objects.getTruncated()); for(ZCObject key : objects.getContents()) { // get the each object details from the list of objects System.out.println("object name: "+key.getKey()); System.out.println("content type: "+key.getContentType()); System.out.println("cached url: "+key.getCachedUrl()); } nextToken = objects.getNextToken(); // get the value of next token }while(nextToken != null); // if next token available continue the iteration else stop the iteration
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.
Ensure the following packages are imported:
copyimport java.util.Iterator; import com.zc.component.stratus.ZCObject; import java.util.List;
copyIterable<List<ZCObject>> paginationIterable=bucket.listIterableObjects(5, "prefix"); Iterator<List<ZCObject>> iterator = paginationIterable.iterator(); while(iterator.hasNext()) { List<ZCObject> obj= iterator.next(); for(ZCObject ob: obj) { System.out.println(ob.getKey()); } }
Last Updated 2025-06-20 16:21:48 +0530 +0530
Yes
No
Send your feedback to us