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. |
orderBy | String | An Optional parameter. To list objects either in ascending or descending order. Default Value: asc |
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.ZCListObjectOptions; import com.zc.component.stratus.beans.ZCPagedObjectResponse; import com.zc.component.stratus.ZCObject;
copyString nextToken = null; String maxKey = "10"; String prefix = "Sam"; do { ZCListObjectOptions options = new ZCListObjectOptions(); options.setMaxKey(maxKey); // Default: 1000 options.setContinuationToken(nextToken); // Fetch next page options.setFolderListing("true"); // Default: false options.setOrderBy("desc"); // Default: "asc" options.setPrefix(prefix); // Optional ZCPagedObjectResponse res = bucket.listPagedObjects(options); System.out.println("Object count: " + res.getKeyCount()); System.out.println("Max key: " + res.getMaxKey()); System.out.println("Is truncated: " + res.getTruncated()); for (ZCObject key : res.getContents()) { System.out.println("Object name: " + key.getKey()); System.out.println("Content type: " + key.getContentType()); System.out.println("Size: " + key.getSize()); System.out.println("Metadata: " + key.getMetaData()); System.out.println("Version ID: " + key.getVersionId()); System.out.println("ETag: " + key.getEtag()); System.out.println("Object type: " + key.getKeyType()); System.out.println("Cached URL: " + key.getCachedUrl()); } nextToken = res.getNextToken(); } while (nextToken != null);
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 com.zc.component.stratus.beans.ZCListObjectOptions; import java.util.List;
copyZCListObjectOptions options = new ZCListObjectOptions(); options.setFolderListing("true"); // Default: false options.setMaxKey("2"); // Default: 1000 options.setOrderBy("desc"); // Default: "asc" // Get iterable object list Iterable<List<ZCObject>> paginationIterable = bucket.listIterableObjects(options); Iterator<List<ZCObject>> iterator = paginationIterable.iterator(); while (iterator.hasNext()) { List<lZCObject> objectList = iterator.next(); for (ZCObject obj : objectList) { System.out.println(obj.getKey()); } }
Last Updated 2025-07-28 16:56:33 +0530 +0530
Yes
No
Send your feedback to us