| title | Object Storage |
|---|---|
| sidebar_position | 4 |
OCI Object Storage stores arbitrary files and objects. Spring Cloud Oracle adds both a storage API and a Spring Resource integration.
<dependency>
<groupId>com.oracle.cloud.spring</groupId>
<artifactId>spring-cloud-oci-starter-storage</artifactId>
</dependency>dependencies {
implementation("com.oracle.cloud.spring:spring-cloud-oci-starter-storage")
}The starter auto-configures a Storage bean for bucket and object operations.
@Autowired
private Storage storage;
public void createBucketAndUploadFile() {
Bucket bucket = storage.createBucket("my-bucket");
storage.upload(
"my-bucket",
"my-file.txt",
inputStream,
StorageObjectMetadata.builder().contentType("text/plain").build());
}Object Storage objects can be accessed using Spring's resource abstraction.
@Value("https://objectstorage.${OCI_REGION}.oraclecloud.com/n/${OCI_NAMESPACE}/b/${OCI_BUCKET}/o/${OCI_OBJECT}")
private Resource myObjectResource;Resource objectResource = applicationContext.getResource(
"https://objectstorage.${OCI_REGION}.oraclecloud.com/n/${OCI_NAMESPACE}/b/${OCI_BUCKET}/o/${OCI_OBJECT}");The resulting Resource can be read like other Spring resources.
try (InputStream inputStream = myObjectResource.getInputStream()) {
String content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}Object Storage resources also implement Spring's WritableResource, so they can be used for write operations as well.
@Value("https://objectstorage.${OCI_REGION}.oraclecloud.com/n/${OCI_NAMESPACE}/b/${OCI_BUCKET}/o/${OCI_OBJECT}")
private WritableResource myWritableObjectResource;You can also resolve a writable object resource programmatically from the application context.
WritableResource writableResource = (WritableResource) applicationContext.getResource(
"https://objectstorage.${OCI_REGION}.oraclecloud.com/n/${OCI_NAMESPACE}/b/${OCI_BUCKET}/o/${OCI_OBJECT}");
try (OutputStream outputStream = writableResource.getOutputStream()) {
outputStream.write("Hello Object Storage".getBytes(StandardCharsets.UTF_8));
}Data written through WritableResource#getOutputStream() is uploaded to OCI Object Storage when the stream is closed.
| Name | Description | Required | Default |
|---|---|---|---|
spring.cloud.oci.storage.enabled |
Enables the OCI Object Storage APIs | No | true |