This repository will not be updated. The repository will be kept available in read-only mode.
In this application, you upload an image to a web application that is stored in IBM Cloud Object Storage, which triggers your serverless functions to run. Those functions perform some image processing and analysis, such as charcoaling the image and running visual recognition on it. After the analysis and processing is done, the results are stored in a different Cloud Object Storage bucket, which can then be read.
- An IBM Cloud Account
- An IBM Cloud CLI with the IBM Cloud Functions Plugin installed.
To run this application, you'll need to set up IBM Object Storage and the IBM Visual Recognition Service on IBM Cloud
-
Create a Cloud Object Storage Service Instance:
- From the catalog select Object Storage.
- Give your service a name, and click
Create
. - In the left side menu, select
Buckets
, and thenCreate bucket
. - Give your bucket a unique name.
- For Resiliency, select
Regional
, and for Location, selectus-south
. Note: This trigger is currently available in us-south, us-east, and eu-gb regions. You could select one of the other available regions, but our examples will use us-south - Click
Create Bucket
. - Create another bucket, with the same name suffixed by
-processed
. If your original bucket wasmy-bucket
, then your new bucket will bemy-bucket-processed
. - Again, ensure that you selected the same region as your first bucket, in this case
Regional
andus-south
. - In the left side menu, click
Service Credentials
. ClickNew Credential
. - Check the checkbox for
Include HMAC Credential
. ClickAdd
.
-
Create a Visual Recognition Service Instance
- From the catalog select Visual Recognition
- Give your service a name, and click
Create
. - In the left side menu, click
Service Credentials
. If there are no service credentials created, clickNew Credential
. Once your Service Credentials are created, make note of yourapikey
.
-
Login to the IBM Cloud CLI:
ibmcloud login
-
List the namespaces available in IBM Cloud Functions:
ibmcloud fn namespace list
-
Set your namespace using the ID found in the previous step:
ibmcloud fn property set --namespace <namespace_id>
- Before you can create a trigger to listen for bucket change events, you must assign the Notifications Manager role to your Cloud Functions namespace. As a Notifications Manager, Cloud Functions can view, modify, and delete notifications for a Cloud Object Storage bucket.
ibmcloud iam authorization-policy-create functions cloud-object-storage "Notifications Manager" --source-service-instance-name <functions_namespace_name> --target-service-instance-name <cos_service_instance_name>
To deploy the functions required in this application, we'll use the ibm fn deploy
command. This command will look for a manifest.yaml
file defining a collection of packages, actions, triggers, and rules to be deployed.
-
Let's clone the application.
git clone [email protected]:IBM/cos-trigger-functions.git
-
Take a look at the
serverless/manifest.yaml file
. You should see manifest describing the various actions, triggers, packages, and sequences to be created. You will also notice that there are a number of environment variables you should set locally before running this manifest file. -
Choose a package name, trigger name, and rule name and then save the environment variables. Note: The package you will create will hold all of the actions for this application.
export PACKAGE_NAME=<your_chosen_package_name> export RULE_NAME=<your_chosen_rule_name> export TRIGGER_NAME=<your_chosen_trigger_name>
-
You already chose a bucket name earlier when creating your COS Instance. Save that name as your BUCKET_NAME environment variable:
export BUCKET_NAME=<your_bucket_name>
-
You will need to save the endpoint name, which is the COS Endpoint for your buckets. Since you selected us-south when selecting your buckets, the endpoint should be
s3.us-south.cloud-object-storage.appdomain.cloud
.export ENDPOINT=s3.us-south.cloud-object-storage.appdomain.cloud
Note: If you selected a different region, you can find your endpoint by clicking your Cloud Object Storage service in the Resource list, finding your bucket in the list, and then looking under Configuration for that bucket. Use the public endpoint.
-
Finally, you will need some information from the Visual Recognition service. You saved your apikey earlier, so use that. This application is built against the version released on
2018-03-19
, so we'll use that value for VERSION.export API_KEY=<your_visual_recognition apikey> export VERSION=2018-03-19
-
You've set up some required credentials and various parameters for your IBM Cloud Functions. Let's deploy the functions now! Change directories to the
serverless
folder, and then deploy the functions.cd serverless ibmcloud fn deploy
-
The deploy command created a package for you called
cloud-object-storage
. This package contains some useful cloud functions for interacting with cloud object storage. Let's update the package with your endpoint information.ibmcloud fn package update cloud-object-storage --param endpoint $ENDPOINT
-
Let's bind the service credentials to this package.
ibmcloud fn service bind cloud-object-storage cloud-object-storage --instance YOUR_COS_INSTANCE_NAME
-
Congratulations! If you went directly to your cloud object storage bucket and added a file, you should see your trigger fire and some processed actions showing up in your
mybucket-processed
bucket. Let's deploy a simple application for uploading the images and showing these results.
Finally, let's deploy the web application that enables our users to upload images and see the resulting images. The application is a node.js with express app, and we can deploy it to IBM Cloud using the manifest.yaml file provided in the /app
folder.
-
Change directories to the
app
folder:cd ../app
-
Update the
config.js
file with the required configuration values: your bucket name, your processed bucket name, and your endpoint url. You should've already found these values earlier. -
Create a file named
credentials.json
based on thecredentials_template.json
file. You can easily get the credentials by going to the cloud object storage service page, and clickingService Credentials
. You can copy this entire block and paste it as a child to"OBJECTSTORAGE_CREDENTIALS":
. -
Open up the manifest.yaml file at
app/manifest.yaml
. You should see something like this:--- applications: - name: <YOUR_APPLICATION_NAME> memory: 256M instances: 1
This manifest file describes the cloud foundry application we're about to deploy. Please rename the application to anything you would like.
-
Deploy the application:
ibmcloud cf push
-
Finally, we'll need to update the CORS settings on our processed images bucket. The
cloud-object-storage
package we installed earlier comes with a method to let us do that. Run the following command, but be sure to edit it first with your own-processed
bucket name.ibmcloud fn action invoke cloud-object-storage/bucket-cors-put -b -p bucket myBucket-processed -p corsConfig "{\"CORSRules\":[{\"AllowedHeaders\":[\"*\"], \"AllowedMethods\":[\"POST\",\"GET\",\"DELETE\"], \"AllowedOrigins\":[\"*\"]}]}"
-
You should now be able to use the application you deployed! When the application was deployed, it should've output a URL where your application lives, something like
my-app-name.mybluemix.net
. You can now go there and start uploading images and seeing your results!