If you want to specify multiple file prefixes and suffixes for an Object Storage Service (OSS) trigger or associate more than 10 triggers with an OSS bucket, you can create an EventBridge-based OSS trigger to handle events such as file uploads with ease.
Usage notes
EventBridge-based OSS triggers are different from native OSS triggers. Take note of the following items when you create an EventBridge-based OSS trigger:
You can associate a maximum of 10 native OSS triggers with a bucket. If you want to associate more OSS triggers with a bucket, you can create and associate EventBridge-based OSS triggers.
NoteWe recommend that you do not associate more than 10 triggers with a bucket. If you want to associate more triggers, we recommend that you create a new bucket and create triggers based on the new bucket.
You can create up to 50 EventBridge-based OSS triggers. This limit is consistent with the corresponding limit in EventBridge. For more information, see Limits.
You do not need to guarantee the semantic uniqueness of EventBridge-based OSS triggers. The semantics specified by the Event Type, Object Prefix, and Object Suffix parameters do not need to be unique.
You can configure multiple entries for Object Prefix and Object Suffix. However, fuzzy match and regular expression match based on Object Prefix and Object Suffix are not supported.
An EventBridge-based OSS trigger does not immediately take effect upon creation. You must wait about 30 seconds before a related OSS event can trigger the associated function.
Sample scenario
You can configure an EventBridge-based OSS trigger and set source and test as prefixes and .rar and .zip suffixes in the trigger rule. When a compressed file is stored in the source or test directory in the specified OSS bucket and the file suffix is .rar or .zip, a function is automatically executed. After the function is executed, the decompressed file is stored in another directory of the same bucket.
Prerequisites
EventBridge
EventBridge is activated, and the required permissions are granted to a Resource Access Management (RAM) user. For more information, see Activate EventBridge and grant permissions to a RAM user.
Function Compute
A service is created. For more information, see Create a service.
A function is created. For more information, see Create a function.
OSS
A bucket is created. For more information, see Create buckets.
Step 1: Create an EventBridge-based OSS trigger
Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click the function that you want to manage.
- On the function details page, click the Triggers tab, select the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
In the Create Trigger panel, select Object Storage Service (OSS) in Event triggers for Alibaba Cloud services from the Trigger Type drop-down list, configure the other parameters, and then click OK.
Parameter
Description
Example
Name
Enter the trigger name.
oss-trigger
Version or Alias
Select an alias or version. Default value: LATEST. If you want to create a trigger for another version or alias, select a version or alias from the Version or Alias drop-down list on the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.
LATEST
Bucket Name
Select a bucket that is created in the current region.
bucket-zh****
Object Prefix
Enter the prefixes of the object names that you want to match. We recommend that you specify the prefixes and suffixes of object names in Function Compute to avoid additional costs caused by nested loops.
You can click + Add File Prefix to add more entries for Object Prefix.
ImportantThe prefix cannot start with a forward slash (
/
). Otherwise, the OSS trigger cannot be triggered.source
test
Object Suffix
Enter the suffixes of the object names that you want to match. We recommend that you specify the prefixes and suffixes of object names in Function Compute to avoid additional costs caused by nested loops.
You can click + Add File Suffix to add more entries for Object Suffix.
png
Event Type
Select one or more trigger events from the drop-down list. For more information about OSS event types, see OSS events.
oss:ObjectCreated:PutObject
,oss:ObjectCreated:PostObject
, andoss:ObjectCreated:CompleteMultipartUpload
Event Pattern Content
After you configure Object Prefix, Object Suffix, and Event Type, the system automatically fills a value for this parameter.
ImportantExercise caution when you modify Event Pattern Content. Otherwise, the trigger may fail to be triggered. Understand the rules of event patterns before you decide whether to modify the event pattern. For more information, see Event patterns.
{ "source":[ "acs.oss" ] }
Invocation Method
Select the function invocation method. Default value: Sync Invocation.
Sync Invocation: After an event triggers a function, Function Compute returns the execution result when the execution is complete. This method is suitable for a wide range of scenarios. For more information, see Synchronous invocations.
Async Invocation: After an event triggers a function, Function Compute immediately returns a response and ensures that the function is executed at least once. However, the detailed execution result is not returned. This invocation method is suitable for functions with higher scheduling latency. For more information, see Overview.
Sync Invocation
Trigger State
Specify whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected, and the trigger is immediately enabled after it is created.
Enable Trigger
Push Format
Specify the data format of input parameters of the function. The data is passed by using the event parameter of the function. Valid values:
CloudEvents: a specification that describes event data in a common format, including event descriptions and event payload data. This specification is used to simplify event declaration and transmission between different services and platforms.
RawData: a specification that delivers only event payload data and does not include other metadata information in the CloudEvents format.
NoteNative OSS triggers and EventBridge-based OSS triggers use different push formats. The event parameter of a native OSS trigger supports the OSSEvents template, which is the event format generated by OSS.
CloudEvents
After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing trigger, see Manage triggers.
Step 2: Configure input parameters of the function
The event sources of EventBridge-based OSS triggers are passed to the function in the CloudEvents format, which acts as an input parameter named event. You can manually pass event to the function to trigger the function.
On the function details page, click the Code tab, click the icon next to Test Function, and then select Configure Test Parameters from the drop-down list.
In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, configure the Event Name parameter, specify the event content, and then click OK.
When a specified event occurs in a specified OSS bucket, the event data is sent to the associated function in JSON format. The event data is passed in the event parameter to Function Compute. The following code provides an example:
{ "datacontenttype": "application/json;charset=utf-8", "aliyunaccountid": "143199913****", "data": { "eventVersion": "1.0", "responseElements": { "requestId": "6364D216511B143733C5A67B" }, "eventSource": "acs:oss", "eventTime": "2023-11-04T08:49:26.000Z", "requestParameters": { "sourceIPAddress": "140.205.XX.XX" }, "eventName": "ObjectCreated:PutObject", "userIdentity": { "principalId": "143199913****" }, "region": "cn-hangzhou", "oss": { "bucket": { "name": "bucket-zh***", "arn": "acs:oss:cn-hangzhou:143199913****:bucket-zh***", "virtualBucket": "", "ownerIdentity": "143199913****" }, "ossSchemaVersion": "1.0", "object": { "size": 13, "objectMeta": { "mimeType": "text/plain" }, "deltaSize": 13, "eTag": "59CA0EFA9F5633CB0371BBC0355478D8", "key": "source/a.png" } } }, "subject": "acs:oss:cn-hangzhou:143199913****:bucket-zh***/source/a.png", "aliyunoriginalaccountid": "143199913****", "source": "acs.oss", "type": "oss:ObjectCreated:PutObject", "aliyunpublishtime": "203-11-04T08:49:26.745Z", "specversion": "1.0", "aliyuneventbusname": "default", "id": "6364D216511B143733C5A67B", "time": "2023-11-04T08:49:26Z", "aliyunregionid": "cn-hangzhou" }
The following table describes the fields in event.
Field
Type
Example
Description
datacontenttype
String
application/json;charset=utf-8
The content type of the data parameter. datacontenttype supports only application/json;charset=utf-8.
aliyunaccountid
String
143199913****
The ID of the Alibaba Cloud account. You can obtain the ID of the Alibaba Cloud account in the References section on the Overview page in the Function Compute console.
data
{}
The OSS event content, which is a JSON object. CloudEvents contain the context that is defined by OSS when the event occurs. This information is encapsulated in the data parameter.
subject
String
acs:oss:cn-hangzhou:143199913****:bucket-zh****/source/a.png
The event subject.
Format:
acs:oss:<region>:<your_account_id>:<your_bucket>
. Set<region>
to the region that you selected when you created the function. Set<your_account_id>
to the ID of your Alibaba Cloud account. Set<your_bucket>
to the name of the bucket that you created in the same region.aliyunoriginalaccountid
String
143199913****
The ID of the Alibaba Cloud account.
source
String
acs.oss
The event source. The value is fixed to
acs.oss
.type
String
oss:ObjectCreated:PutObject
The event type.
aliyunpublishtime
Timestamp
2023-11-04T08:49:26.745Z
The time when the event was received.
specversion
String
1.0
The version of the CloudEvents specification.
aliyuneventbusname
String
default
The name of the event bus that receives the event.
id
String
6364D216511B143733C5A67B
The event ID.
time
Timestamp
2023-11-04T08:49:26Z
The time when the event occurred.
aliyunregionid
String
cn-hangzhou
The region in which the event was received.
aliyunpublishaddr
String
140.205.XX.XX
The IP address of the server that receives the event.
Configure the data field of the Event parameter based on your business requirements.
userIdentity.principalId
: the ID of your Alibaba Cloud account.region
: the region that you selected when you created the function.oss.bucket.name
: the name of the created bucket in the same region as the function.oss.bucket.arn
: in theacs:oss:<region>:<your_account_id>:<your_bucket>
format. Set<region>
to the region that you selected when you created the function. Set<your_account_id>
to the ID of your Alibaba Cloud account. Set<your_bucket>
to the name of the bucket that you created in the same region.oss.bucket.ownerIdentity
: the ID of your Alibaba Cloud account.object.key
: the name of the object that you have uploaded to the destination bucket.NoteMake sure that the specified object exists in the specified bucket. Otherwise, the function cannot be executed or the execution fails. In this example, source/a.png is used.
Step 3: Write function code and test the function
After you create the trigger, you can write function code and test the function to verify whether the code is correct. When an OSS event occurs, the function is automatically triggered.
You must avoid loop triggers in the code. Otherwise, unnecessary costs are incurred. For example, when objects are uploaded to an OSS bucket, the upload operation triggers a function. Then, this function generates one or more objects to be written to the OSS bucket, and the write operation triggers this function again. This leads to a loop. For more information, see Triggering rules.
On the function details page, click the Code tab, enter function code in the code editor, and then click Deploy.
The following section describes the preparations that you must make and the sample code:
NoteIf you want to read data from and write data to OSS resources in your function, we recommend that you use an internal endpoint of OSS to access the OSS resources. If you use the public endpoint, you are charged for the data transferred over the Internet. For more information about the format of OSS internal endpoints, see Regions and endpoints.
/* Preparations: 1. Run the following command on the client to create the package.json file and install the jimp module. a. Run the npm init command on the client to create the package.json file. b. Run the npm install jimp command to install the jimp module. 2. Make sure that handler of the function is index.handler. */ 'use strict'; console.log('Loading function ...'); var oss = require('ali-oss'); var fs = require('fs'); var jimp = require("jimp"); module.exports.handler = function (eventBuf, ctx, callback) { console.log('Received event:', eventBuf.toString()); var event = JSON.parse(eventBuf); var ossEvent = event.data; // OSS regions are prefixed with oss-. Example: oss-cn-shanghai. var ossRegion = "oss-" + ossEvent.region; // Create an OSS client. var client = new oss({ region: ossRegion, /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey secret to your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. In this example, the AccessKey ID and AccessKey secret are obtained from the context. */ accessKeyId: ctx.credentials.accessKeyId, accessKeySecret: ctx.credentials.accessKeySecret, stsToken: ctx.credentials.securityToken }); // Obtain the bucket name from the event. client.useBucket(ossEvent.oss.bucket.name); // The processed image is saved to the processed/ directory. var newKey = ossEvent.oss.object.key.replace("source/", "processed/"); var tmpFile = "/tmp/processed.png"; // Obtain the OSS object. console.log('Getting object: ', ossEvent.oss.object.key) client.get(ossEvent.oss.object.key).then(function (val) { // Read the content of the OSS object from the cache. jimp.read(val.content, function (err, image) { if (err) { console.error("Failed to read image"); callback(err); return; } // Resize the image and save it to the tmp file. image.resize(128, 128).write(tmpFile, function (err) { if (err) { console.error("Failed to write image locally"); callback(err); return; } // Upload the read OSS object to the OSS bucket and rename the object. console.log('Putting object: ', newKey); client.put(newKey, tmpFile).then(function (val) { console.log('Put object:', val); callback(null, val); return; }).catch(function (err) { console.error('Failed to put object: %j', err); callback(err); return; }); }); }); }).catch(function (err) { console.error('Failed to get object: %j', err); callback(err); return }); };
# Preparations: #1. Make sure that the RAM role configured for the service to which the function belongs has the permissions to access OSS. You can log on to the RAM console and grant the role permissions to access OSS. #2. Make sure that handler of the function is index.handler. # -*- coding: utf-8 -*- import oss2, json from wand.image import Image def handler(event, context): evt = json.loads(event) creds = context.credentials # Required by OSS sdk #The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. # We recommend that you do not save the AccessKey ID and AccessKey secret to your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. # In this example, the AccessKey ID and AccessKey secret are obtained from the context. auth=oss2.StsAuth( creds.access_key_id, creds.access_key_secret, creds.security_token) evt = evt['data'] bucket_name = evt['oss']['bucket']['name'] endpoint = 'oss-' + evt['region'] + '-internal.aliyuncs.com' bucket = oss2.Bucket(auth, endpoint, bucket_name) objectName = evt['oss']['object']['key'] # Processed images will be saved to processed/ newKey = objectName.replace("source/", "processed/") remote_stream = bucket.get_object(objectName) if not remote_stream: return remote_stream = remote_stream.read() with Image(blob=remote_stream) as img: with img.clone() as i: i.resize(128, 128) new_blob = i.make_blob() bucket.put_object(newKey, new_blob)
/* Preparations: 1. Make sure that the RAM role configured for the service to which the function belongs has the permissions to access OSS. You can log on to the RAM console and grant the role permissions to access OSS. 2. Make sure that handler of the function is index.handler. */ <?php use OSS\OssClient; function handler($event, $context) { $event = json_decode($event, $assoc = true); /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey secret to your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. In this example, the Access Key pair is obtained from the context. */ $accessKeyId = $context["credentials"]["accessKeyId"]; $accessKeySecret = $context["credentials"]["accessKeySecret"]; $securityToken = $context["credentials"]["securityToken"]; $evt = $event['data']; $bucketName = $evt['oss']['bucket']['name']; $endpoint = 'oss-' . $evt['region'] . '-internal.aliyuncs.com'; $objectName = $evt['oss']['object']['key']; $newKey = str_replace("source/", "processed/", $objectName); try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken); $content = $ossClient->getObject($bucketName , $objectName); if ($content == null || $content == "") { return; } $imagick = new Imagick(); $imagick->readImageBlob($content); $imagick->resizeImage(128, 128, Imagick::FILTER_LANCZOS, 1); $ossClient->putObject($bucketName, $newKey, $imagick->getImageBlob()); } catch (OssException $e) { print($e->getMessage()); } }
On the Code tab, click Test Function.
After the execution is complete, you can view the execution result on the Code tab.
FAQ
References
For more information about the OSS event types that you can configure, see OSS events.
If you want to modify or delete existing triggers, see Modify the configurations of a trigger.
Triggers
If you want to check which event triggers a function, you can configure the system to print event type logs in your code. For more information, see Log records.
If you want to use a function to invoke another function, you can specify an API operation to invoke the function or use CloudFlow to orchestrate functions. If the invoked function requires a long time to execute and involves asynchronous invocations, you can configure destination services for asynchronous invocations. For more information, see Can functions invoke each other? and Configure a destination for an asynchronous invocation.