Skip to content

Exclude Slash from Encode, when calling getURL() on S3 Resource #353

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

Merged
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 @@ -23,6 +23,9 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.WritableResource;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -76,7 +79,11 @@ public S3Resource(Location location, S3Client s3Client, S3OutputStreamProvider s

@Override
public URL getURL() throws IOException {
String encodedObjectName = URLEncoder.encode(location.getObject(), StandardCharsets.UTF_8.toString());
List<String> splits = new ArrayList<>();
for (String split : location.getObject().split("/")) {
splits.add(URLEncoder.encode(split, StandardCharsets.UTF_8.toString()));
}
String encodedObjectName = String.join("/", splits);
return new URL("https", location.getBucket() + ".s3.amazonaws.com", "/" + encodedObjectName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ void returnsResourceUrl() throws IOException {
void returnsEncodedResourceUrlAndUri() throws IOException, URISyntaxException {
S3Resource resource = s3Resource("s3://first-bucket/some/[objectName]");
assertThat(resource.getURL().toString())
.isEqualTo("https://first-bucket.s3.amazonaws.com/some%2F%5BobjectName%5D");
.isEqualTo("https://first-bucket.s3.amazonaws.com/some/%5BobjectName%5D");
assertThat(resource.getURI())
.isEqualTo(new URI("https://first-bucket.s3.amazonaws.com/some%2F%5BobjectName%5D"));
.isEqualTo(new URI("https://first-bucket.s3.amazonaws.com/some/%5BobjectName%5D"));
}

@Test
Expand Down