Skip to content

Commit

Permalink
Exclude slash from encoding, when calling getURL() on S3 Resource (#353)
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Rosauro <[email protected]>
  • Loading branch information
willome and Guillaume Rosauro authored Apr 29, 2022
1 parent e362bf6 commit 22c961d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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

0 comments on commit 22c961d

Please sign in to comment.