Skip to content

Latest commit

 

History

History

oci-objectstorage-put-object-java

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Function that creates an object in a bucket in Object Storage using the OCI Java SDK

This function uses Resource Principals to securely authorize a function to make API calls to OCI services using the OCI Java SDK. It creates an object in a bucket in Object Storage and returns a message with a status.

The function calls the following OCI Java SDK classes:

As you make your way through this tutorial, look out for this icon user input icon. Whenever you see it, it's time for you to perform an action.

Prerequisites

  1. Before you deploy this sample function, make sure you have run steps A, B and C of the Oracle Functions Quick Start Guide for Cloud Shell

    • A - Set up your tenancy
    • B - Create application
    • C - Set up your Cloud Shell dev environment
  2. Have your Oracle Object Storage Namespace available. This can be found by logging into your cloud account, under your user profile, click on your Tenancy. Your Object Storage Namespace is shown there.

List Applications

Assuming you have successfully completed the prerequisites, you should see your application in the list of applications.

fn ls apps

Create or Update your Dynamic Group

In order to use other OCI Services, your function must be part of a dynamic group. For information on how to create a dynamic group, refer to the documentation.

When specifying the Matching Rules, we suggest matching all functions in a compartment with:

ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}

Create or Update IAM Policies

Create a new policy that allows the dynamic group to manage objects in the functions related compartment.

user input icon

Your policy should look something like this:

Allow dynamic-group <dynamic-group-name> to manage objects in compartment <compartment-name>

e.g.

Allow dynamic-group demo-func-dyn-group to manage objects in compartment demo-func-compartment

For more information on how to create policies, go here.

Review and customize the function

Review the following files in the current folder:

The name of your function oci-objectstorage-put-object-java is specified in func.yaml.

Deploy the function

In Cloud Shell, run the fn deploy command to build the function and its dependencies as a Docker image, push the image to the specified Docker registry, and deploy the function to Oracle Functions in the application created earlier:

user input icon

fn -v deploy --app <app-name>

e.g.

fn -v deploy --app myapp

Set function configuration values

The function requires the config value NAMESPACE to be set.

user input icon

Use the fn CLI to set the config value at the application level (if multiple functions need the same config value):

fn config app <app-name> NAMESPACE <object-storage-namespace>

e.g.

fn config app myapp NAMESPACE myobjnamespace

Note that the config value can also be set at the function level.

Create a bucket called "test"

user input icon

From the OCI Console > Core Infrastructure > Object Storage > Create Bucket with bucket name = "test"

Test

Use the fn CLI to invoke your function with your own bucket name and app name:

user input icon

echo -n '{"name": "<object-name>", "bucketName":"<bucket-name>", "content": "<text-content>"}' | fn invoke <app-name> <function-name>

e.g.

echo -n '{"name": "file1.txt", "bucketName":"mybucket", "content": "This file was created in OCI object storage bucket using Oracle Functions"}' | fn invoke myapp oci-objectstorage-put-object-java

You should see a success message appear in your terminal.

Monitoring Functions

Learn how to configure basic observability for your function using metrics, alarms and email alerts: