Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make getUrl return location of bucket if object is empty or null. #1158

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading