我正在尝试从包含特定字符串的S3的关键对象列表中过滤出来。这是我的方法:
public List<String>getBucketList3(String filterStr) {
List<String> lst = new ArrayList<>();
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName);
ObjectListing objectListing = s3client.listObjects(listObjectsRequest);
for (; ; ) {
List<S3ObjectSummary> summaries = objectListing.getObjectSummaries();
if (summaries.size() < 1) {
break;
}
summaries.forEach(s -> lst.add(s.getKey()));
objectListing = s3client.listNextBatchOfObjects(objectListing);
}
return lst;
}
我正在尝试仅过滤包含" filterstr"的关键对象,然后将其添加到我的" lst"数组列表字符串中。我正在尝试使用Java 8的流()来完成它,但是我正在努力解决它。
您可以尝试这样的事情:
ObjectListing objectListing = s3client.listObjects(listObjectsRequest);
List<S3ObjectSummary> summaries = objectListing.getObjectSummaries();
List<String> lst = new ArrayList<>();
do {
lst.addAll(summaries.stream()
.map(S3ObjectSummary::getKey) // creates a string stream
.filter(s -> s.contains(filterStr)) // filters all values containing filterStr
.collect(Collectors.toList()));
objectListing = s3client.listNextBatchOfObjects(objectListing);
summaries = objectListing.getObjectSummaries();
} while (!summaries.isEmpty());
List<String> list = lst.stream()
.filter(s -> s.contains(filterStr))
.collect(Collectors.toList());
如果这是您的情况的正确谓词,它可以这样工作。