|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | + |
| 3 | + Copyright (c) 2021 Oracle and/or its affiliates. |
| 4 | + |
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | + |
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + |
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | + |
| 17 | +/////////////////////////////////////////////////////////////////////////////// |
| 18 | +
|
| 19 | +:javadoc-base-url-api: {javadoc-base-url}io.helidon.config/io/helidon/oci |
| 20 | +
|
| 21 | += OCI Object Storage |
| 22 | +:h1Prefix: MP |
| 23 | +:description: Helidon OCI Object Storage integration |
| 24 | +:keywords: oci, objectstorage |
| 25 | +:common-deps-page-prefix-inc: ../../shared/dependencies/common_shared.adoc |
| 26 | +:feature-name: OCI Object Storage |
| 27 | +
|
| 28 | +The Helidon MP OCI Object Storage integration provides easy access files stored in Oracle cloud. |
| 29 | +
|
| 30 | +== Experimental |
| 31 | +
|
| 32 | +WARNING: Helidon integration with Oracle Cloud Infrastructure is still experimental and not intended for production use. APIs and features have not yet been fully tested and are subject to change. |
| 33 | +
|
| 34 | +include::{common-deps-page-prefix-inc}[tag=maven-dependency] |
| 35 | +
|
| 36 | +[source,xml] |
| 37 | +---- |
| 38 | +<dependency> |
| 39 | + <groupId>io.helidon.integrations.oci</groupId> |
| 40 | + <artifactId>helidon-integrations-oci-objectstorage</artifactId> |
| 41 | +</dependency> |
| 42 | +---- |
| 43 | +
|
| 44 | +
|
| 45 | +== Setting up the Object Storage |
| 46 | +
|
| 47 | +In order to use the OCI Object Storage integration, the following setup should be made: |
| 48 | +
|
| 49 | +Current configuration requires `~/.oci/config` to be available in the home folder. This configuration file can be downloaded from OCI. |
| 50 | +
|
| 51 | +REST endpoint to work with OCI Object Storage: |
| 52 | +
|
| 53 | +[source,java] |
| 54 | +---- |
| 55 | +@Path("/files") |
| 56 | +public class ObjectStorageResource { |
| 57 | + private final OciObjectStorage objectStorage; |
| 58 | + private final String bucketName; |
| 59 | +
|
| 60 | + @Inject |
| 61 | + ObjectStorageResource(OciObjectStorage objectStorage, <1> |
| 62 | + @ConfigProperty(name = "oci.objectstorage.bucket") |
| 63 | + String bucketName) { <2> |
| 64 | + this.objectStorage = objectStorage; |
| 65 | + this.bucketName = bucketName; |
| 66 | + } |
| 67 | +} |
| 68 | +---- |
| 69 | +<1> `OciObjectStorage` is configured and injected automatically |
| 70 | +<2> Bucket name is read from the properties |
| 71 | +
|
| 72 | +Additionally, in `microprofile-config.properties` OCI properties should be specified: |
| 73 | +
|
| 74 | +[source,properties] |
| 75 | +---- |
| 76 | +oci.properties.compartment-ocid: "ocid1.tenancy.oc1..<..>" |
| 77 | +oci.properties.objectstorage-namespace: "<..>" |
| 78 | +oci.properties.objectstorage-bucket: "demobucket" |
| 79 | +---- |
| 80 | +
|
| 81 | +The exact values are available in OCI object storage and bucket properties. |
| 82 | +
|
| 83 | +image::oci/ocibucket.png[OCI Bucket, align="center"] |
| 84 | +
|
| 85 | +== Using Object Storage |
| 86 | +
|
| 87 | +=== Upload file |
| 88 | +
|
| 89 | +To upload a file to OCI Object Storage using the `PUT` method: |
| 90 | +
|
| 91 | +[source,java] |
| 92 | +---- |
| 93 | +@POST |
| 94 | +@Path("/file/{fileName}") |
| 95 | +public Response upload(@PathParam("fileName") String fileName, |
| 96 | + @HeaderParam("Content-Length") long contentLength, |
| 97 | + @HeaderParam("Content-Type") @DefaultValue("application/octet-stream") String type, |
| 98 | + InputStream entity) { |
| 99 | + PutObject.Response response = objectStorage.putObject(PutObject.Request.builder() <1> |
| 100 | + .contentLength(contentLength) |
| 101 | + .bucket(bucketName) |
| 102 | + .requestMediaType(io.helidon.common.http.MediaType |
| 103 | + .parse(type)) |
| 104 | + .objectName(fileName), |
| 105 | + Channels.newChannel(entity)); |
| 106 | +
|
| 107 | + return Response.status(response.status().code()) <2> |
| 108 | + .header("opc-request-id", response.headers().first("opc-request-id").orElse("")) |
| 109 | + .header("request-id", response.requestId()) |
| 110 | + .build(); |
| 111 | +} |
| 112 | +---- |
| 113 | +
|
| 114 | +<1> Use `objectStorage.putObject` method with`PutObject.Request.builder()` to submit data |
| 115 | +<2> Put the result in the `Response` |
| 116 | +
|
| 117 | +
|
| 118 | +=== Download file |
| 119 | +
|
| 120 | +To download a file from OCI Object Storage using the `GET` method: |
| 121 | +
|
| 122 | +[source,java] |
| 123 | +---- |
| 124 | +@GET |
| 125 | +@Path("/file/{file-name}") |
| 126 | +public Response download(@PathParam("file-name") String fileName) { |
| 127 | + ApiOptionalResponse<GetObject.Response> ociResponse = objectStorage.getObject(GetObject.Request.builder() <1> |
| 128 | + .bucket(bucketName) |
| 129 | + .objectName(fileName)); |
| 130 | + Optional<GetObject.Response> entity = ociResponse.entity(); <2> |
| 131 | +
|
| 132 | + if (entity.isEmpty()) { |
| 133 | + return Response.status(Response.Status.NOT_FOUND).build(); <3> |
| 134 | + } |
| 135 | +
|
| 136 | + GetObject.Response response = entity.get(); |
| 137 | +
|
| 138 | + StreamingOutput stream = output -> response.writeTo(Channels.newChannel(output)); |
| 139 | +
|
| 140 | + Response.ResponseBuilder ok = Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM_TYPE) <4> |
| 141 | + .header(Http.Header.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"") |
| 142 | + .header("opc-request-id", ociResponse.headers().first("opc-request-id").orElse("")) |
| 143 | + .header("request-id", ociResponse.requestId()); |
| 144 | +
|
| 145 | + ociResponse.headers() |
| 146 | + .first(Http.Header.CONTENT_TYPE) |
| 147 | + .ifPresent(ok::type); |
| 148 | +
|
| 149 | + ociResponse.headers() |
| 150 | + .first(Http.Header.CONTENT_LENGTH) |
| 151 | + .ifPresent(it -> ok.header(Http.Header.CONTENT_LENGTH, it)); |
| 152 | +
|
| 153 | + return ok.build(); |
| 154 | +} |
| 155 | +---- |
| 156 | +
|
| 157 | +<1> Use `getObject` function to make asynchronous request to OCI Object Storage |
| 158 | +<2> The result is of type `Optional` |
| 159 | +<3> Whenever the result is empty, return status `404` |
| 160 | +<4> Get the response, set headers and return the result |
| 161 | +
|
| 162 | +
|
| 163 | +=== Delete file |
| 164 | +
|
| 165 | +Finally, to delete a file, `DELETE` request should be used: |
| 166 | +
|
| 167 | +[source,java] |
| 168 | +---- |
| 169 | +@DELETE |
| 170 | +@Path("/file/{file-name}") |
| 171 | +public Response delete(@PathParam("file-name") String fileName) { |
| 172 | + DeleteObject.Response response = objectStorage.deleteObject(DeleteObject.Request.builder() <1> |
| 173 | + .bucket(bucketName) |
| 174 | + .objectName(fileName)); |
| 175 | +
|
| 176 | + return Response.status(response.status().code()) <2> |
| 177 | + .header("opc-request-id", response.headers().first("opc-request-id").orElse("")) |
| 178 | + .header("request-id", response.requestId()) |
| 179 | + .build(); |
| 180 | +} |
| 181 | +---- |
| 182 | +
|
| 183 | +<1> Use `deleteObject` function and configure a `DeleteObject.Request.builder()` to submit the delete request |
| 184 | +<2> Return the result |
0 commit comments