Post files to OCI Object Storage Bucket via API Gateway image 23

Post files to OCI Object Storage Bucket via API Gateway

In a previous article I discussed accessing a static web application (a Vue 3 application) stored on OCI Object Storage through API Gatyeway. In this article I used a Pre Authenticated Request for a Bucket on Object Storage to allow API Gateway read access to the files in the bucket.

In this article I will use a different type of Pre Authenticated Request – one that allows writing of files to a bucket. Using such a PAR it is straightforward to allow API Gateway to create files based on incoming requests. No code needs to be written. And no direct access to the bucket needs to be provided to external clients: they access API Gateway and in turn API Gateway interacts with the bucket (if it trusts and accepts the request).

When all is said and done, I will be able to send a POST request to API Gateway – with content or an attachment such as an image –and have a file created in the designated Object Storage Bucket.

image

When this is up and running, I can use this channel for quickly uploading files to OCI Object Storage, for example as a simple persistent back end for web applications, in lieu of a database.

The steps to implement this:

  1. Prepare a bucket and a write-enabled Pre Authenticated Request
  2. Try out the PAR: upload a file using CURL through the PAR to the bucket
  3. Prepare an API Gateway
  4. Create a Deployment on the API Gateway that uses the write-enabled Pre Authenticated Request as its backend and the request header with the file name to define the object name
  5. Try out POST requests to the API Gateway endpoint and see if files are created in the bucket

Now for some action.

Prepare a bucket and a write-enabled Pre Authenticated Request

Creating a bucket is really very straightforward. A quick screenshot:image

Creation of the Pre Authenticated Request that allows files to be written is also simple enough:

image

Essential elements: the fact that the scope is bucket and that writing is allowed. Also the expiration date – one year into the future.

When I click create, I get the URL I need to later on access the bucket through POST requests to upload files:

image

Try out the PAR: upload a file using CURL through the PAR to the bucket

I have a local file – socialFabric.jpg – and I want this file to end up in the bucket. Using cURL this is simple enough:

image

and the result is seen in the OCI Console:

image

Prepare an API Gateway

See this article for the steps to create a new API Gateway (and a VCN if needed).

Create a Deployment on the API Gateway to write files in the Bucket

I need to create a deployment that uses the write-enabled Pre Authenticated Request as its backend and the request header with the file name to define the object name.

I create a new deployment with /submit as path prefix:

image

No authentication (for now):

image

Then a route to handle PUT and POST requests at / – or any request with /submit/ at this deployment’s endpoint (URL)

image

The Backend Type is HTTP. The URL is defined as:

PAR/${request.headers[file-name]}

the expression ${request.headers[file-name]} is replaced with the value of the request header called file-name. The backend’s endpoint now looks like this:

https://idtwlqf2hanz.objectstorage.us-ashburn-1.oci.customer-oci.com/p/XAP95_q/n/idtwlqf2hanz/b/uploads/o/${request.headers[file-name]}

Click Next. Inspect the configuration and if OK then press Create

image

Once the Deployment is created, get its endpoint:

image

Try out POST requests to the API Gateway endpoint and see if files are created in the bucket

Using cURL I can make this call that will go to API Gateway and through the PAR creates a file in the bucket with content of my local file socialFabric.jpg::

curl -X PUT -T “socialFabric.jpg” -H “Content-Type: image/jpeg”  -H “file-name: some-image.jpg” https://i5gzhagd2523536dvsje.apigateway.us-ashburn-1.oci.customer-oci.com/submit/

image

and the resulting file in the bucket:

image

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.