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

GetURL fix to 3.0.5 #1226

Merged
merged 1 commit into from
Sep 22, 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 @@ -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
Loading