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

Commit 58e636d

Browse files
giova333maciejwalkowiak
giova333
authored andcommitted
Fix S3 object key encoding in SimpleStorageResource.getURL.
Fixes gh-483 Closes gh-562
1 parent f2ab807 commit 58e636d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

spring-cloud-aws-core/src/main/java/org/springframework/cloud/aws/core/io/s3/SimpleStorageResource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
2626
import java.net.URL;
27+
import java.net.URLEncoder;
28+
import java.nio.charset.StandardCharsets;
2729
import java.security.MessageDigest;
2830
import java.security.NoSuchAlgorithmException;
2931
import java.util.ArrayList;
@@ -139,8 +141,10 @@ public String getFilename() throws IllegalStateException {
139141
@Override
140142
public URL getURL() throws IOException {
141143
Region region = this.amazonS3.getRegion().toAWSRegion();
144+
String encodedObjectName = URLEncoder.encode(this.objectName,
145+
StandardCharsets.UTF_8.toString());
142146
return new URL("https", region.getServiceEndpoint(AmazonS3Client.S3_SERVICE_NAME),
143-
"/" + this.bucketName + "/" + this.objectName);
147+
"/" + this.bucketName + "/" + encodedObjectName);
144148
}
145149

146150
@Override

spring-cloud-aws-core/src/test/java/org/springframework/cloud/aws/core/io/s3/SimpleStorageResourceTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
24+
import java.net.URI;
2425
import java.net.URL;
2526
import java.util.Date;
2627

@@ -297,4 +298,15 @@ public void writeFile_forNewFile_writesFileContent() throws Exception {
297298
// Assert
298299
}
299300

301+
@Test
302+
public void getUri_encodes_objectName() throws Exception {
303+
AmazonS3 s3 = mock(AmazonS3.class);
304+
when(s3.getRegion()).thenReturn(Region.US_West_2);
305+
SimpleStorageResource resource = new SimpleStorageResource(s3, "bucketName",
306+
"some/[objectName]", new SyncTaskExecutor());
307+
308+
assertThat(resource.getURI()).isEqualTo(new URI(
309+
"https://s3.us-west-2.amazonaws.com/bucketName/some%2F%5BobjectName%5D"));
310+
}
311+
300312
}

0 commit comments

Comments
 (0)