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(),