Skip to content

Commit

Permalink
Make getUrl return location of bucket if object is empty or null. (#…
Browse files Browse the repository at this point in the history
…1226)

Back-ported from 3.2.0. Original commit id: a7a07d5
  • Loading branch information
MatejNedic committed Sep 22, 2024
1 parent 635a405 commit ca63f30
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 @@ -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(),
Expand Down

0 comments on commit ca63f30

Please sign in to comment.