Triggers are used to call functions. In an event-driven computing model, the event source is the event raiser, the function is the event handler, and the trigger centrally manages different event sources. When an event that matches the trigger-defined rules occurs, the event source calls the function corresponding to the trigger.
In some cases, you may not want to run a function by directly calling it. For example, if an application is using Object Storage Service (OSS) for storing the images that you upload, you can call a function to download and process the images, and then record the results in OSS or other services. If OSS can detect newly uploaded images and automatically call the corresponding functions, you do not need to call functions manually. In this way, the development and application processes are simplified.
Function Compute provides an event-driven computing model. Function execution can be triggered in the Function Compute console, the command-line tool fcli, the SDK, or other event sources. A Function Compute trigger describes a set of rules. When an event meets these rules, the event source calls a corresponding function. Currently, Function Compute supports the following event sources.
triggerName: Indicates the name of the trigger.
triggerType: Indicates the type of the trigger, such as the OSS trigger, Time trigger, and HTTP trigger.
sourceArn: Indicates the event source that triggers a function execution. This property is required when other services of Alibaba Cloud are involved in calling functions of Function Compute, such as the OSS trigger and Log Service trigger. If the Time trigger, HTTP trigger, or other triggers are used, and no other Alibaba Cloud service is involved, this property is not required. For example, the value format of the OSS trigger, is
invocationRole: Indicates the role used by the event source to call the function. The role grants event source the permission to call functions. For more information about roles of event sources, see Introduction.
qualifier：Optional. Indicates service version or alias. For more information about qualifier usage, see version management.
triggerConfig: Specifies the configurations of the trigger. For more information about the configurations of different triggers, see the corresponding documents.
The following example shows how to create an OSS trigger. When an OSS bucket detects that a new image is uploaded and the image is prefixed with source/, a function is called to run the resize operation. The image generated subsequently is saved to the /processed directory.
An OSS bucket for storing images is available. This bucket has the /source directory, under which the image 1.jpg used for debugging is stored.
For more information about creating a function, see Basic operations. The following provides an example code. The Python runtime environments of Function Compute provide an image processing library named wand. Therefore, no third-party dependencies need to be uploaded.
# -*- coding: utf-8 -*-
import oss2, json
from wand.image import Image
def resize(event, context):
evt = json.loads(event)
creds = context.credentials
# Required by OSS SDK
evt = evt['events']
bucket_name = evt['oss']['bucket']['name']
endpoint = 'oss-' + evt['region'] + '.aliyuncs.com'
bucket = oss2.Bucket(auth, endpoint, bucket_name)
objectName = evt['oss']['object']['key']
# Processed images are saved to processed/
newKey = objectName.replace("source/", "processed/")
remote_stream = bucket.get_object(objectName)
if not remote_stream:
remote_stream = remote_stream.read()
with Image(blob=remote_stream) as img:
with img.clone() as i:
new_blob = i.make_blob()
Before testing a trigger, you can debug it by triggering an event source. First, you can create a
source directory under the OSS bucket and upload the image 1.jpg to this directory. Then, set an event source to call a function.
On the code execution page, click Trigger.
In the Test Event dialog box, select OSS Template, and edit your
keyparameters in the event template accordingly.
Click Invoke. A directory named
processedis created under the OSS bucket and the processed image 1.jpg is stored in this directory. This process simulates how a function is executed in Function Compute when an object is created in the /source directory of the OSS bucket.
In the OSS console, upload an image to the /source directory of the OSS bucket configured for the trigger. A resized image with the same name is generated in the /processed directory.