Skip to content

Commit 38c5b26

Browse files
Storage uses plain Object Storage URLs (#66)
* OCI Storage uses plain object storage URLs Signed-off-by: Anders Swanson <anders.swanson@oracle.com> --------- Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent 5b1bc67 commit 38c5b26

File tree

14 files changed

+82
-132
lines changed

14 files changed

+82
-132
lines changed

docs/src/main/asciidoc/storage.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The `Storage` bean (link[Javadoc]) can be used to list, create, update or delete
3737
private Storage storage;
3838
3939
public void createBucketAndUploadFile() {
40-
Bucket bucket = storage.createBucket("my-bucket")
40+
Bucket bucket = storage.createBucket("my-bucket");
4141
4242
storage.upload("my-bucket", "my-file.txt", inputStream, StorageObjectMetadata.builder().contentType("text/plain").build());
4343
}
@@ -46,19 +46,19 @@ public void createBucketAndUploadFile() {
4646
=== Cloud Storage Objects Spring Resources
4747

4848
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html[Spring Resources] are an abstraction for several low-level resources, such as file system files, classpath files, servlet context-relative files, etc.
49-
Spring Cloud OCI adds a new resource type: an Oracle Cloud Storage (OCS) object.
49+
Spring Cloud OCI adds a new resource type for Oracle Cloud Object Storage.
5050

51-
The Spring Resource Abstraction for Oracle Cloud Storage allows OCS objects to be accessed by their OCS URL using the `@Value` annotation or the Spring application context:
51+
The Spring Resource Abstraction for Oracle Cloud Storage allows Object Storage objects to be accessed by their URL using the `@Value` annotation or the Spring application context:
5252

5353
[source,java]
5454
----
55-
@Value("ocs://[YOUR_OCS_BUCKET]/[OCS_FILE_NAME]")
56-
private Resource ocsResource;
55+
@Value("https://objectstorage.us-chicago-1.oraclecloud.com/n/${OCI_NAMESPACE}/b/${OCI_BUCKET}/o/${OCI_OBJECT}")
56+
private Resource myObjectResource;
5757
----
5858

5959
[source,java]
6060
----
61-
SpringApplication.run(...).getResource("ocs://[YOUR_OCS_BUCKET]/[OCS_FILE_NAME]");
61+
SpringApplication.run(...).getResource("Object Storage URL");
6262
----
6363

6464
This creates a `Resource` object that can be used to read the object, among https://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html#resources-resource[other possible operations].

spring-cloud-oci-samples/spring-cloud-oci-storage-sample/src/main/java/com/oracle/cloud/spring/sample/storage/springcloudocistoragesample/ImageController.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ ResponseEntity<Resource> download(@Parameter(required = true, example = "new-buc
5454
@PostMapping(value = "/{bucketName}", consumes = MULTIPART_FORM_DATA_VALUE)
5555
ResponseEntity<?> upload(@Parameter(required = true) @RequestPart(required = true, name = "file") MultipartFile multipartFile,
5656
@Parameter(required = true, example = "new-bucket") @PathVariable String bucketName) throws IOException {
57-
//if (!IMAGE_JPEG.toString().equals(multipartFile.getContentType())) {
58-
// return ResponseEntity.badRequest().body("Invalid image file");
59-
// }
60-
6157
try (InputStream is = multipartFile.getInputStream()) {
6258
storage.upload(bucketName, multipartFile.getOriginalFilename(), is,
6359
StorageObjectMetadata.builder().contentType(multipartFile.getContentType()).build());

spring-cloud-oci-samples/spring-cloud-oci-storage-sample/src/main/java/com/oracle/cloud/spring/sample/storage/springcloudocistoragesample/ObjectController.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.swagger.v3.oas.annotations.tags.Tag;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.beans.factory.annotation.Value;
14+
import org.springframework.core.io.Resource;
1415
import org.springframework.core.io.ResourceLoader;
1516
import org.springframework.web.bind.annotation.DeleteMapping;
1617
import org.springframework.web.bind.annotation.GetMapping;
@@ -33,18 +34,11 @@ public class ObjectController {
3334
@Autowired
3435
ResourceLoader loader;
3536

36-
@Value("ocs://your-bucket/555.json")
37-
Object myObject;
37+
@Value("https://objectstorage.us-chicago-1.oraclecloud.com/n/${OCI_NAMESPACE}/b/${OCI_BUCKET}/o/${OCI_OBJECT}")
38+
Resource myObject;
3839

3940
@GetMapping("/")
40-
String hello() throws IOException {
41-
// Note: This is the sample piece of code to access
42-
// OCI Storage Object with resource URI as ocs://<bucket>/<object-name>
43-
44-
// Object obj = loader.getResource("ocs://your-bucket/object-name");
45-
// Scanner s = new Scanner(((OracleStorageResource)obj).getInputStream()).useDelimiter("\\A");
46-
// String result = s.hasNext() ? s.next() : "";
47-
// System.out.println(result);
41+
String hello() {
4842
return "Hello World ";
4943
}
5044

spring-cloud-oci-samples/spring-cloud-oci-storage-sample/src/main/resources/application.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring:
2+
cloud:
3+
oci:
4+
compartment:
5+
static: ${OCI_COMPARTMENT}
6+
config:
7+
type: file
8+
region:
9+
static: us-chicago-1

spring-cloud-oci-samples/spring-cloud-oci-storage-sample/src/test/java/com/oracle/cloud/spring/sample/storage/springcloudocistoragesample/SpringCloudOciStorageSampleApplicationTests.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,43 @@
55

66
package com.oracle.cloud.spring.sample.storage.springcloudocistoragesample;
77

8-
import com.oracle.cloud.spring.sample.common.base.SpringCloudSampleApplicationTestBase;
98
import com.oracle.cloud.spring.storage.Storage;
9+
import org.junit.jupiter.api.Disabled;
1010
import org.junit.jupiter.api.Test;
11-
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
11+
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.boot.test.context.SpringBootTest;
14-
import org.springframework.test.context.TestPropertySource;
14+
import org.springframework.core.io.Resource;
1515

1616
import java.io.IOException;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
19+
1820
@SpringBootTest
19-
@EnabledIfSystemProperty(named = "it.storage", matches = "true")
20-
@TestPropertySource(locations="classpath:application-test.properties")
21-
class SpringCloudOciStorageSampleApplicationTests extends SpringCloudSampleApplicationTestBase {
22-
static final String TEST_BUCKET = "bucketName";
21+
@EnabledIfEnvironmentVariable(named = "OCI_NAMESPACE", matches = ".+")
22+
@EnabledIfEnvironmentVariable(named = "OCI_BUCKET", matches = ".+")
23+
@EnabledIfEnvironmentVariable(named = "OCI_OBJECT", matches = ".+")
24+
@EnabledIfEnvironmentVariable(named = "OCI_COMPARTMNET", matches = ".+")
25+
class SpringCloudOciStorageSampleApplicationTests {
2326

24-
static final String testBucket = System.getProperty(TEST_BUCKET) != null ? System.getProperty(TEST_BUCKET) :
25-
System.getenv().get(TEST_BUCKET);
27+
static final String testBucket = System.getenv("OCI_BUCKET");
2628

2729
@Autowired
2830
Storage storage;
2931

32+
@Autowired
33+
ObjectController objectController;
34+
35+
@Test
36+
void resourceIsLoaded() throws IOException {
37+
Resource myObject = objectController.myObject;
38+
assertThat(myObject).isNotNull();
39+
assertThat(myObject.getContentAsByteArray()).hasSizeGreaterThan(1);
40+
}
41+
3042
@Test
31-
void testFileUpload() throws IOException {
43+
@Disabled
44+
void storePOJO() throws IOException {
3245
ActivityInfo ainfo = new ActivityInfo("Hello from Storage integration test");
3346
storage.store(testBucket, ainfo.getFileName(), ainfo);
3447
}

spring-cloud-oci-samples/spring-cloud-oci-storage-sample/src/test/resources/application-test.properties

Lines changed: 0 additions & 9 deletions
This file was deleted.

spring-cloud-oci-storage/src/main/java/com/oracle/cloud/spring/storage/OracleStorageProtocolResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
6969
((DefaultResourceLoader) resourceLoader).addProtocolResolver(this);
7070
} else {
7171
LOGGER.warn("The provided delegate resource loader is not an implementation "
72-
+ "of DefaultResourceLoader. Custom Protocol using ocs:// prefix will not be enabled.");
72+
+ "of DefaultResourceLoader. Custom OCI Object Storage prefix will not be enabled.");
7373
}
7474
}
7575

spring-cloud-oci-storage/src/main/java/com/oracle/cloud/spring/storage/OracleStorageResource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import com.oracle.bmc.objectstorage.responses.GetNamespaceResponse;
1212
import com.oracle.bmc.objectstorage.responses.GetObjectResponse;
1313
import org.springframework.core.io.AbstractResource;
14+
import org.springframework.core.io.WritableResource;
1415
import org.springframework.lang.Nullable;
1516

1617
import java.io.IOException;
1718
import java.io.InputStream;
19+
import java.io.OutputStream;
1820

1921
/**
2022
* Default OCI Storage resource implementation of Spring Resource.
@@ -40,9 +42,9 @@ public static OracleStorageResource create(String location, ObjectStorageClient
4042
return null;
4143
}
4244

43-
public OracleStorageResource(String bucketName, String objectName, String version,
45+
public OracleStorageResource(String bucketName, String objectName,
4446
ObjectStorageClient osClient) {
45-
this(new StorageLocation(bucketName, objectName, version), osClient);
47+
this(new StorageLocation(bucketName, objectName), osClient);
4648
}
4749

4850
public OracleStorageResource(StorageLocation location, ObjectStorageClient osClient) {
@@ -76,7 +78,6 @@ public InputStream getInputStream() throws IOException {
7678
.namespaceName(namespaceName)
7779
.bucketName(location.getBucket())
7880
.objectName(location.getObject())
79-
.versionId(location.getVersion())
8081
.build());
8182
return getResponse.getInputStream();
8283
}

spring-cloud-oci-storage/src/main/java/com/oracle/cloud/spring/storage/StorageImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public OracleStorageResource download(String bucketName, String key, String vers
6565
Assert.notNull(bucketName, ERROR_BUCKET_NAME_REQUIRED);
6666
Assert.notNull(key, ERROR_KEY_REQUIRED);
6767

68-
return new OracleStorageResource(bucketName, key, version, osClient);
68+
return new OracleStorageResource(bucketName, key, osClient);
6969
}
7070

7171
/**

0 commit comments

Comments
 (0)