Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Issue 483: SimpleStorageResource.getURL should encode s3 object key #562

Closed
Closed
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 @@ -24,6 +24,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
Expand Down Expand Up @@ -139,8 +141,10 @@ public String getFilename() throws IllegalStateException {
@Override
public URL getURL() throws IOException {
Region region = this.amazonS3.getRegion().toAWSRegion();
String encodedObjectName = URLEncoder.encode(this.objectName,
StandardCharsets.UTF_8.toString());
return new URL("https", region.getServiceEndpoint(AmazonS3Client.S3_SERVICE_NAME),
"/" + this.bucketName + "/" + this.objectName);
"/" + this.bucketName + "/" + encodedObjectName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.util.Date;

Expand Down Expand Up @@ -297,4 +298,15 @@ public void writeFile_forNewFile_writesFileContent() throws Exception {
// Assert
}

@Test
public void getUri_encodes_objectName() throws Exception {
AmazonS3 s3 = mock(AmazonS3.class);
when(s3.getRegion()).thenReturn(Region.US_West_2);
SimpleStorageResource resource = new SimpleStorageResource(s3, "bucketName",
"some/[objectName]", new SyncTaskExecutor());

assertThat(resource.getURI()).isEqualTo(new URI(
"https://s3.us-west-2.amazonaws.com/bucketName/some%2F%5BobjectName%5D"));
}

}