Skip to content

Commit

Permalink
Make getUrl return location of bucket if object is empty or null. (#1158
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MatejNedic authored Sep 13, 2024
1 parent 7d4e7e5 commit a7a07d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ void returnsResourceUrl(S3OutputStreamProvider s3OutputStreamProvider) throws IO
.isEqualTo("http://127.0.0.1:" + localstack.getFirstMappedPort() + "/first-bucket/a-file.txt");
}

@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 returnsEncodedResourceUrlAndUri(S3OutputStreamProvider s3OutputStreamProvider)
throws IOException, URISyntaxException {
Expand Down

0 comments on commit a7a07d5

Please sign in to comment.