From ca63f309b3ca65b43dade71f2c614247b68405f6 Mon Sep 17 00:00:00 2001 From: MatejNedic Date: Sun, 22 Sep 2024 21:53:07 +0200 Subject: [PATCH] Make `getUrl` return location of bucket if object is empty or null. (#1226) Back-ported from 3.2.0. Original commit id: a7a07d5eaa00978ced9e26b9c736b584fd29c611 --- .../src/main/java/io/awspring/cloud/s3/S3Resource.java | 4 ++++ .../io/awspring/cloud/s3/S3ResourceIntegrationTests.java | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/S3Resource.java b/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/S3Resource.java index c125f02d0..42a089913 100644 --- a/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/S3Resource.java +++ b/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/S3Resource.java @@ -26,6 +26,7 @@ import org.springframework.core.io.WritableResource; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetUrlRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; @@ -82,6 +83,9 @@ public S3Resource(Location location, S3Client s3Client, S3OutputStreamProvider s @Override public URL getURL() throws IOException { + if (!StringUtils.hasText(this.location.getObject())) { + return new URL("https", location.getBucket() + ".s3.amazonaws.com", "/"); + } GetUrlRequest getUrlRequest = GetUrlRequest.builder().bucket(this.getLocation().getBucket()) .key(this.location.getObject()).versionId(this.location.getVersion()).build(); return s3Client.utilities().getUrl(getUrlRequest); diff --git a/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java b/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java index 23a22ecb9..2604b157c 100644 --- a/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java +++ b/spring-cloud-aws-s3-parent/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java @@ -97,6 +97,13 @@ static void beforeAll() { client.createBucket(request -> request.bucket("first-bucket")); } + @TestAvailableOutputStreamProviders + void returnsEmptyUrlToBucketWhenObjectIsEmpty(S3OutputStreamProvider s3OutputStreamProvider) throws IOException { + S3Resource resource = s3Resource("s3://first-bucket/", s3OutputStreamProvider); + assertThat(resource.getURL().toString()) + .isEqualTo("https://first-bucket.s3.amazonaws.com/"); + } + @TestAvailableOutputStreamProviders void readsFileFromS3(S3OutputStreamProvider s3OutputStreamProvider) throws IOException { client.putObject(PutObjectRequest.builder().bucket("first-bucket").key("test-file.txt").build(),