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

Commit 2a59abf

Browse files
tmnuwan12maciejwalkowiak
authored andcommitted
Add a method to get S3 URI from a SimpleStorageResource.
Fixes gh-361 Closes gh-565
1 parent 211990c commit 2a59abf

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.IOException;
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
2628
import java.net.URL;
2729
import java.net.URLEncoder;
2830
import java.nio.charset.StandardCharsets;
@@ -147,6 +149,15 @@ public URL getURL() throws IOException {
147149
"/" + this.bucketName + "/" + encodedObjectName);
148150
}
149151

152+
public URI getS3Uri() {
153+
try {
154+
return new URI("s3", "//" + this.bucketName + "/" + this.objectName, null);
155+
}
156+
catch (URISyntaxException e) {
157+
throw new RuntimeException("Failed to resolve s3:// uri", e);
158+
}
159+
}
160+
150161
@Override
151162
public File getFile() throws IOException {
152163
throw new UnsupportedOperationException(

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ public void getUrl_existingObject_returnsUrlWithS3Prefix() throws Exception {
232232

233233
}
234234

235+
@Test
236+
public void getUrl_existingObject_returnsUrlWithS3Scheme() throws Exception {
237+
238+
AmazonS3Client amazonS3 = mock(AmazonS3Client.class);
239+
240+
// Act
241+
SimpleStorageResource simpleStorageResource = new SimpleStorageResource(amazonS3,
242+
"bucket", "object", new SyncTaskExecutor());
243+
244+
// Assert
245+
assertThat(simpleStorageResource.getS3Uri())
246+
.isEqualTo(new URI("s3://bucket/object"));
247+
248+
}
249+
235250
@Test
236251
public void getFile_existingObject_throwsMeaningFullException() throws Exception {
237252

0 commit comments

Comments
 (0)