File tree 3 files changed +46
-0
lines changed
spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src
main/java/io/awspring/cloud/s3
test/java/io/awspring/cloud/s3
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 28
28
* {@link S3Template}.
29
29
*
30
30
* @author Maciej Walkowiak
31
+ * @author Ziemowit Stolarczyk
31
32
* @since 3.0
32
33
*/
33
34
public interface S3Operations {
@@ -70,6 +71,15 @@ public interface S3Operations {
70
71
*/
71
72
void deleteObject (String s3Url );
72
73
74
+ /**
75
+ * Checks if an S3 object exists.
76
+ *
77
+ * @param bucketName - the bucket name
78
+ * @param key - the object key
79
+ * @return true if object exists; false otherwise
80
+ */
81
+ boolean objectExists (String bucketName , String key );
82
+
73
83
/**
74
84
* Returns some or all (up to 1,000) of the objects in a bucket. Does not handle pagination. If you need pagination
75
85
* you should use {@link S3PathMatchingResourcePatternResolver} or {@link S3Client}
Original file line number Diff line number Diff line change 28
28
import software .amazon .awssdk .services .s3 .model .ListObjectsV2Request ;
29
29
import software .amazon .awssdk .services .s3 .model .ListObjectsV2Response ;
30
30
import software .amazon .awssdk .services .s3 .model .NoSuchBucketException ;
31
+ import software .amazon .awssdk .services .s3 .model .NoSuchKeyException ;
31
32
import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
32
33
import software .amazon .awssdk .services .s3 .presigner .S3Presigner ;
33
34
import software .amazon .awssdk .services .s3 .presigner .model .GetObjectPresignRequest ;
39
40
* Higher level abstraction over {@link S3Client} providing methods for the most common use cases.
40
41
*
41
42
* @author Maciej Walkowiak
43
+ * @author Ziemowit Stolarczyk
42
44
* @since 3.0
43
45
*/
44
46
public class S3Template implements S3Operations {
@@ -101,6 +103,19 @@ public void deleteObject(String s3Url) {
101
103
this .deleteObject (location .getBucket (), location .getObject ());
102
104
}
103
105
106
+ @ Override
107
+ public boolean objectExists (String bucketName , String key ) {
108
+ Assert .notNull (bucketName , "bucketName is required" );
109
+ Assert .notNull (key , "key is required" );
110
+ try {
111
+ s3Client .headObject (request -> request .bucket (bucketName ).key (key ));
112
+ }
113
+ catch (NoSuchBucketException | NoSuchKeyException e ) {
114
+ return false ;
115
+ }
116
+ return true ;
117
+ }
118
+
104
119
@ Override
105
120
public List <S3Resource > listObjects (String bucketName , String prefix ) {
106
121
Assert .notNull (bucketName , "bucketName is required" );
Original file line number Diff line number Diff line change 61
61
*
62
62
* @author Maciej Walkowiak
63
63
* @author Yuki Yoshida
64
+ * @author Ziemowit Stolarczyk
64
65
*/
65
66
@ Testcontainers
66
67
class S3TemplateIntegrationTests {
@@ -164,6 +165,26 @@ void deletesObjectByS3Url() {
164
165
.isThrownBy (() -> client .headObject (r -> r .bucket (BUCKET_NAME ).key ("key.txt" )));
165
166
}
166
167
168
+ @ Test
169
+ void whenObjectExistsShouldReturnTrue () {
170
+ client .putObject (r -> r .bucket (BUCKET_NAME ).key ("key.txt" ), RequestBody .fromString ("hello" ));
171
+
172
+ final boolean existsObject = s3Template .objectExists (BUCKET_NAME , "key.txt" );
173
+
174
+ assertThat (existsObject ).isTrue ();
175
+ }
176
+
177
+ @ Test
178
+ void whenObjectNotExistsShouldReturnFalse () {
179
+ client .putObject (r -> r .bucket (BUCKET_NAME ).key ("key.txt" ), RequestBody .fromString ("hello" ));
180
+
181
+ final boolean existsObject1 = s3Template .objectExists (BUCKET_NAME , "other-key.txt" );
182
+ final boolean existsObject2 = s3Template .objectExists ("other-bucket" , "key.txt" );
183
+
184
+ assertThat (existsObject1 ).isFalse ();
185
+ assertThat (existsObject2 ).isFalse ();
186
+ }
187
+
167
188
@ Test
168
189
void listObjects () throws IOException {
169
190
client .putObject (r -> r .bucket (BUCKET_NAME ).key ("hello-en.txt" ), RequestBody .fromString ("hello" ));
You can’t perform that action at this time.
0 commit comments