Use OCI Object Storage data as Spring Boot Resources

Anders Swanson
2 min readJul 31, 2024

--

Spring Resources are an excellent to load application data, supporting filepaths, URLs, classpath objects, and other custom data formats.

With Spring Cloud Oracle Storage, you can load data from Oracle Cloud Infrastructure (OCI) Object Storage by settings the Spring Resource value to an OCI Object Storage URL.

Spring Cloud Oracle Storage will parse the URL to download the object from OCI Object Storage, presenting the object to your application through the Spring Resource API.

@Value("${OBJECT_STORAGE_URI}")
Resource myObject;

Configuring Spring Cloud Oracle Storage

To use Spring Cloud Oracle Storage, we’ll need a bucket in our OCI account with at least one object in it, and our Object Storage namespace value.

If you don’t already have a bucket set up, follow these steps to create a bucket, add an object, and fetch your tenancy’s Object Storage namespace.

  1. Create an OCI Object Storage bucket.
  2. Upload an object to a bucket.
  3. Get your tenancy’s namespace.

Once your bucket is setup and at least one object has been uploaded, get the uploaded object’s URL from the console by navigating to the object in your bucket, and clicking “View Object Details”

In the object details window, copy the object URL in the new format as shown. While either URL format may be used with Spring Cloud Oracle Storage, I recommend using the newer one.

Next, configure Spring Cloud Oracle to authenticate to OCI in your Spring Boot application’s properties. In this example, we’ll use config file authentication, but User Principal, Instance Principal, or Workload Identity may also be used.

spring:
cloud:
oci:
compartment:
static: ${OCI_COMPARTMENT}
config:
type: file
region:
static: ${OCI_REGION}

Tying it together with a simple Spring Boot Service

Let’s write a simple Spring Boot Service that loads an object as a resource, using the URL of the object we created earlier. In this example, we’ve set the OCI Object Storage object URL as an environment variable so the URL isn’t stored directly in our application.

@Service
public class ObjectStorageService {
@Value("${OCI_OBJECT_URL}")
private Resource myObject;

public byte[] getObjectBytes() throws IOException {
return myObject.getContentAsByteArray();
}
}

That’s all there is to it! I hope you enjoyed this post, and can use Spring Cloud Oracle to better integrate your applications with OCI services.

--

--