我正在使用AWS Java SDK进行开发。我想通过上次修改日期来获得一个带有过滤器(如过滤器)的对象列表。我可以在s3api上看到以下功能
aws s3api list-objects
--bucket "myS3-BucketName"
--query "Contents[?LastModified>=`2018-02-01`].{Key: Key, Size: Size, LastModified: LastModified}"
--max-items 10"
我在Java SDK中找不到类似的解决方案。如何使用Java SDK进行这项工作?
答案 0 :(得分:1)
使用适用于Java的AWS开发工具包的v2,创建了以下实用程序方法:
/**
* 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
获取所需的数据: