使用 AWS S3 SDK 为 Java 2.x 列出对象

List objects with AWS S3 SDK for Java 2.x

我在 Amazon S3 (us-east-1) 中有一个存储桶 (logs),不出所料,其中包含按应用程序和日期分区的日志:

logs
├── peacekeepers
│   └── year=2018
│       ├── month=11
│       │   ├── day=01
│       │   ├── day=…
│       │   └── day=30
│       └── month=12
│           ├── day=01
│           ├── day=…
│           └── day=19
│               ├── 00:00 — 01:00.log
│               ├── …
│               └── 23:00 — 00:00.log
├── rep-hunters
├── retro-fans
└── rubber-duckies

我想列出特定日期、月份、年份的所有对象(日志)……

如何使用 AWS SDK for Java 2.x 做到这一点?

新的 SDK 可以轻松处理分页结果:

S3Client client = S3Client.builder().region(Region.US_EAST_1).build();
ListObjectsV2Request request =
        ListObjectsV2Request
                .builder()
                .bucket("logs")
                .prefix("peacekeepers/year=2018/month=12")
                // .prefix("peacekeepers/year=2018/month=12/day=19")
                .build();
ListObjectsV2Iterable response = client.listObjectsV2Paginator(request);

for (ListObjectsV2Response page : response) {
    for (S3Object object : page.contents()) {
        // Consume the object
        System.out.println(object.key());
    }
}