Используя v2 пакета AWS SDK для Java, я создал следующий служебный метод:
/**
* Gets S3 objects that reside in a specific bucket and whose keys conform to the
* specified prefix using v2 of the AWS Java SDK.
* <br><br>
* The objects returned will have a last-modified date between {@code start} and
* {@code end}.
* <br><br>
* Any objects that have been modified outside of the specified date-time range will
* not be returned.
*
* @param s3Client The v2 AWS S3 client used to make the request to S3.
* @param bucket The bucket where the S3 objects are located.
* @param prefix The common prefix that the keys of the S3 objects must conform to.
* @param start The objects returned will have been modified after this instant.
* @param end The objects returned will have been modified before this instant.
* @return A {@link Stream} of {@link S3Object} objects.
*/
public static Stream<S3Object> getObjects(S3Client s3Client, String bucket,
String prefix, Instant start,
Instant end) {
return s3Client.listObjectsV2Paginator(builder -> builder.bucket(bucket)
.prefix(prefix).build())
.stream()
.map(ListObjectsV2Response::contents)
.flatMap(List::stream)
.filter(s3Object -> {
Instant lastModified = s3Object.lastModified();
return !start.isAfter(lastModified) && !end.isBefore(lastModified);
});
}
Следующий код логически эквивалентен вашему примеру:
S3Client s3Client = S3Client.create();
String bucket = "myS3-BucketName";
Instant before = Instant.parse("2018-02-01T00:00:00Z");
Instant after = Instant.MAX;
Stream<S3Object> firstTenObjects =
getObjects(s3Client, bucket, "", before, after).limit(10);
Вы можете использовать следующие методы для получения данных, которые вы ищете от каждого S3Object
в Stream
: