阿里云OSSClient分页获取文件列表,支持上一页,下一页,跳转页
解决方法:
思路就是先遍历统计出总数,同时把nextMarker和页码关联起来放到map中,前端使用页码就可以找到nextMarker,例如下面查询,查询bucket为pub ,目录为dir的所有文件,分页,每页10条,
OSSClient ossClient = new OSSClient(endpoint, accessId, accessKey);
ObjectListing objectListing =null;
int total=0;
HashMap<Integer, String> markerMap = new HashMap<>();
try {
ObjectListing objectListing2 =null;
do {
String nextMarker2=objectListing2!=null?objectListing2.getNextMarker():null;
ListObjectsRequest listObjectsRequest2= new ListObjectsRequest("pub").withMarker(nextMarker2).withMaxKeys(10);
listObjectsRequest2.setPrefix("dir");
objectListing2 =ossClient.listObjects(listObjectsRequest2);
total+=(objectListing2!=null&&objectListing2.getObjectSummaries()!=null?objectListing2.getObjectSummaries().size():0);
markerMap.put(markerMap.size()+1,nextMarker2);
}while (objectListing2!=null&&!StringUtils.isEmpty(objectListing2.getNextMarker()));
ListObjectsRequest listObjectsRequest= new ListObjectsRequest("pub").withMarker(nextMarker).withMaxKeys(10;
listObjectsRequest.setPrefix("dir");
objectListing =ossClient.listObjects(listObjectsRequest);
} catch (Exception e) {
} finally {
// 关闭client
ossClient.shutdown();
}