Skip to content

Commit 31b708b

Browse files
authored
Add listAllObjects method to S3Template (#1443)
1 parent f9fc4a2 commit 31b708b

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/S3Operations.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public interface S3Operations {
8080
*/
8181
boolean objectExists(String bucketName, String key);
8282

83+
/**
84+
* Returns some or all (up to 1,000) of the objects in a bucket. Does not handle pagination. If you need pagination
85+
* you should use {@link S3PathMatchingResourcePatternResolver} or {@link S3Client}
86+
*
87+
* @param bucketName - the bucket name
88+
* @return list of {@link S3Resource}
89+
*/
90+
List<S3Resource> listAllObjects(String bucketName);
91+
8392
/**
8493
* Returns some or all (up to 1,000) of the objects in a bucket. Does not handle pagination. If you need pagination
8594
* you should use {@link S3PathMatchingResourcePatternResolver} or {@link S3Client}

spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/S3Template.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public boolean objectExists(String bucketName, String key) {
116116
return true;
117117
}
118118

119+
@Override
120+
public List<S3Resource> listAllObjects(String bucketName) {
121+
return listObjects(bucketName, "");
122+
}
123+
119124
@Override
120125
public List<S3Resource> listObjects(String bucketName, String prefix) {
121126
Assert.notNull(bucketName, "bucketName is required");

spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3TemplateIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ void whenObjectNotExistsShouldReturnFalse() {
190190
assertThat(existsObject2).isFalse();
191191
}
192192

193+
@Test
194+
void listAllObjects() throws IOException {
195+
client.putObject(r -> r.bucket(BUCKET_NAME).key("hello-en.txt"), RequestBody.fromString("hello"));
196+
client.putObject(r -> r.bucket(BUCKET_NAME).key("hello-fr.txt"), RequestBody.fromString("bonjour"));
197+
client.putObject(r -> r.bucket(BUCKET_NAME).key("bye.txt"), RequestBody.fromString("bye"));
198+
199+
List<S3Resource> resources = s3Template.listAllObjects(BUCKET_NAME);
200+
assertThat(resources.size()).isEqualTo(3);
201+
202+
// According to the S3Client doc : "Objects are returned sorted in an ascending order of the respective key
203+
// names in the list."
204+
assertThat(resources).extracting(S3Resource::getInputStream)
205+
.map(is -> new String(is.readAllBytes(), StandardCharsets.UTF_8))
206+
.containsExactly("bye", "hello", "bonjour");
207+
}
208+
193209
@Test
194210
void listObjects() throws IOException {
195211
client.putObject(r -> r.bucket(BUCKET_NAME).key("hello-en.txt"), RequestBody.fromString("hello"));

0 commit comments

Comments
 (0)