×


Basic steps for creating an IBM cloud Function (Node.js Runtime) that writes to IBM Object Storage Bucket

This is a simple tutorial which helps to understand the basic of IBM Cloud functions and Object Storage. For actual development purposes, things including architecture, security factors should be addressed.

 

 

step1: Choose a Function Namespace and create Action

Namespaces contain entities (e.g. actions and triggers) and belong to a resource group. actions imply the code that can be invoked with HTTP requests.

Navigate to Functions->Actions -> Create Action with Node.js as Runtime. Choose the action name and Package Name. Keep the default code for now. The function main will have the logic you want to run in the function.

 

 

 

step:2 Create API

Navigate to API and click on Create API choose options and create an API. Chose the package and the action that was created in the previous step.

Create an operation with the verb. The verb indicates the HTTPS method.

 

 

 

 

Step3: Create Object Storage and  Bucket

Now the Bucket needs to be created. Navigate to resources and click on Create Object. Provide necessary details like name and create object storage.

 

 

In Object storage, create a bucket.

Choose the region as us-east for now

Create service credentials.

After creating service credentials, copy it.  A service credential will have the following structure.

 

Navigate to your bucket, access-policies->serviceIDs

Create a policy with your serviceID and role as Writer

 

 

 

Get the endpoint URL from configuration

 

 

 

Step4: Modify function code 

The function created in the earlier step should have the following code.

Do the following mapping for the config file from the service credentials in the previous step

ibm-cos-sdk  is the IBM Cloud Object Storage , Node.js SDK

 

Mapping  service credential ->config

apikey  -> apiKeyId
endpoint -> Endpoint from the previous step
iam_apikey_description ->iam_apikey_description
iam_apikey_name -> iam_apikey_name
iam_role_crn -> iam_role_crn
iam_serviceid_crn -> iam_serviceid_crn
resource_instance_id ->resource_instance_id

 

 

 

 

Step 5: Testing in postman

Copy the endpoint and path and execute it in Postman. The gateway can be also tried out in API section.

 

Now in Postman

Navigate to objects in the interface and see the inserted object.

 

This is just an introduction to IBM resources – functions, and Object Storage. People with knowledge in AWS Lambda and s3Bucket will find it easy to understand.

 

Thanks For reading

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *