ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(bucketName) .withPrefix(prefix) ; ObjectListing objects = s3Client.listObjects(listObjectsRequest); ...
【补充 1012-08-21】
按目录层次查询:虽然S3服务器上并无目录的概念,但是在List接口上添加了按层次查询的支持,具体就是Prefix和Delimiter的结合,一般Delimiter就是斜线/。具体解释一下,就是如果存在:
china/beijing/a.txt
china/beijing/b.txt
china/anhui/c.txt
china/all.txt
想列出china/下的第一级层次,可以设置prefix="china/", delimiter="/",通过ObjectListing的getCommonprefixes()得到china/beijing/, china/anhui/共两项(子目录),通过getObjectSummaries()得到china/all.txt (叶子节点)。
ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(bucketName) .withPrefix(prefix) .withDelimiter("/") ; ObjectListing objects = s3Client.listObjects(listObjectsRequest); do { for(String objKey : objects.getCommonPrefixes()) { System.out.println("+ " + objKey); } for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) { String objKey = objectSummary.getKey(); //keyList.add(objKey); System.out.println(objKey); } objects = s3Client.listNextBatchOfObjects(objects); } while (objects.isTruncated());