To set multiple file prefixes and suffixes for an OSS trigger or associate more than 10 triggers with a single OSS bucket, you can create an EventBridge-based OSS trigger to handle events such as file uploads.
Important notes
An EventBridge-based OSS trigger differs from a native OSS trigger. Note the following points:
A native OSS trigger supports up to 10 triggers per bucket. If you need more than 10 OSS triggers in one bucket, use an EventBridge-based OSS trigger.
NoteIn general, do not associate more than 10 triggers with a single bucket. If you require more triggers, create a new bucket and configure the triggers for that bucket.
EventBridge-based OSS triggers follow the same resource limits as EventBridge. You can create up to 50 EventBridge-based OSS triggers. For more information, see Limits.
EventBridge-based OSS triggers do not require semantic uniqueness. The combination of event type, file prefix, and file suffix does not need to be unique.
EventBridge-based OSS triggers support multiple file prefixes and file suffixes. However, file prefixes and file suffixes do not support fuzzy matching or regular expression matching.
After you create an EventBridge-based OSS trigger, it takes about 30 seconds to become active. Object Storage Service (OSS) operations can trigger the function only after the trigger is active.
Example scenario
This example shows how to configure an OSS trigger with the prefixes source and test and the suffixes .rar and .zip. When a compressed file with a .rar or .zip suffix is uploaded to the source or test directory in the specified OSS bucket, the function is automatically executed. After the function is executed, the decompressed files are saved to another directory in the same bucket.
Prerequisites
EventBridge
Function Compute
Object Storage Service (OSS)
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, set Trigger Type to Object Storage Service (OSS) under Cloud Service Event Trigger. Configure the remaining settings and click OK.
Configuration item
Action
Example in this topic
Name
Enter a custom trigger name.
oss-trigger
Version or Alias
The default value is LATEST. To create a trigger for another version or alias, first select it 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 an OSS bucket created in the current region.
bucket-zh****
File prefix
Enter the prefix of the file name to match. Function Compute recommends configuring both prefix and suffix to avoid nested loops that cause extra charges.
To add multiple file prefixes, click + Add File Prefix in the console.
ImportantThe file prefix must not start with
/. Otherwise, the OSS trigger will not activate.source
test
File suffix
Enter the suffix of the file name to match. Function Compute recommends configuring both prefix and suffix to avoid nested loops that cause extra charges.
To add multiple file suffixes, click + Add File Suffix in the console.
png
Event type
Select one or more trigger events. For Object Storage Service (OSS) event types, see OSS Event Definitions.
oss:ObjectCreated:PutObject,oss:ObjectCreated:PostObject,oss:ObjectCreated:CompleteMultipartUploadEvent pattern
This field auto-fills after you configure file prefix, file suffix, and triggering event.
ImportantDo not modify the event pattern unless necessary. Incorrect changes may cause the trigger to fail. Understand the event pattern rules before making changes. For more information, see Event patterns.
{ "source":[ "acs.oss" ] }Invocation method
Select a function invocation method. The default is synchronous call.
Synchronous call: The event triggers the function and waits for the result. This method suits most scenarios. For more information, see Synchronous invocation.
Asynchronous call: The event triggers the function and returns immediately. Function Compute ensures the function executes at least once but does not return the result. Use this for functions with long delays. For more information, see Overview.
Synchronous call
Trigger status
Choose whether to enable the trigger immediately after creation. By default, Enable trigger is selected.
Enable trigger
Payload format
Specify the data format of the function's input parameter. The data passes through the function's Event parameter.
CloudEvents: A standard format for describing event data. It includes event metadata and payload to simplify event declaration and transmission across services and platforms.
RawData: Delivers only the event payload without CloudEvents metadata.
NoteThe payload format differs between native OSS triggers and EventBridge-based OSS triggers. Native OSS triggers support the OSSEvents template, which is the event format generated by Alibaba Cloud Object Storage Service (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 the function input parameter
An EventBridge-based OSS trigger passes event data to the function using the CloudEvents template. You can manually pass an event to the function to simulate a triggering event.
On the function details page, click the Function Code tab. Then, click the
icon to the right of Test Function, and select Configure Test Parameters from the drop-down list.In the Configure Test Parameters panel, select either the Create New Test Event tab or the Edit Existing Test Event tab. Enter an event name and the event content. Then click OK.
The event is the input parameter for Function Compute. When a specified event occurs in the OSS bucket, the event data is sent to the bound function in JSON format. The format is as follows:
{ "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 the event parameter.
Parameter
Type
Example value
Description
datacontenttype
String
application/json;charset=utf-8
The content type of the data parameter. datacontenttype only supports application/json;charset=utf-8.
aliyunaccountid
String
143199913****
Alibaba Cloud account ID. You can find your Alibaba Cloud account ID in the General Information section on the Overview page of the Function Compute console.
data
{}
OSS event content as a JSON object. CloudEvents wraps the OSS-defined context in the data field.
subject
String
acs:oss:cn-hangzhou:143199913****:bucket-zh****/source/a.png
Event subject.
Format:
acs:oss:<region>:<your_account_id>:<your_bucket>. Replace<region>with the region selected when creating the function,<your_account_id>with your Alibaba Cloud account ID, and<your_bucket>with the actual bucket name in the same region.aliyunoriginalaccountid
String
143199913****
Alibaba Cloud account ID.
source
String
acs.oss
Event source. For OSS triggers, this is always
acs.oss.type
String
oss:ObjectCreated:PutObject
Event type.
aliyunpublishtime
Timestamp
2023-11-04T08:49:26.745Z
Time when the event was received.
specversion
String
1.0
CloudEvents protocol version.
aliyuneventbusname
String
default
Name of the event bus that receives the event.
id
String
6364D216511B143733C5A67B
Event ID.
time
Timestamp
2023-11-04T08:49:26Z
Time when the event occurred.
aliyunregionid
String
cn-hangzhou
Region where the event was received.
aliyunpublishaddr
String
140.205.XX.XX
Server address that received the event.
Update the following fields in the data section of the event parameter based on your configurations.
userIdentity.principalId: Replace with your Alibaba Cloud account ID.region: Replace this with the region that you selected when you created the function.oss.bucket.name: Replace this with the name of the bucket in the same region.oss.bucket.arn: The format isacs:oss:<region>:<your_account_id>:<your_bucket>. Replace<region>with the region that you selected when you created the function,<your_account_id>with your Alibaba Cloud account ID, and<your_bucket>with the name of the bucket in the same region.oss.bucket.ownerIdentity: Replace with your Alibaba Cloud account ID.object.key: Replace this with the actual file that is uploaded to the target bucket.NoteMake sure that the specified file exists in the configured bucket. In this example, the file is source/a.png. Otherwise, the function is not triggered or fails to be executed.
Step 3: Write and test the function code
After you create the OSS trigger, you can write and test the function code to verify its correctness. The function is automatically executed when an OSS event occurs.
To prevent unnecessary charges, avoid circular triggers in your code. A common circular trigger scenario is when a file upload to an OSS bucket triggers a function, and the function then writes one or more files back to the same bucket. This action triggers the function again and creates a loop. For more information, see OSS trigger rules.
On the function details page, click the Code tab. Write your code in the editor, and then click Deploy Code.
The following section describes the preparations and provides code examples.
NoteIf your function needs to read data from or write data to OSS resources, we recommend that you use the OSS internal endpoint to avoid public network fees. For more information about the formats of OSS internal endpoints, see Regions and endpoints.
/* Preparations: 1. Run the following commands in your terminal to install package.json and the jimp image processing module. a. Run npm init to create package.json. b. Run npm install jimp to install the jimp module. 2. Ensure the function handler 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 region uses the "oss-" prefix, for example, "oss-cn-shanghai". var ossRegion = "oss-" + ossEvent.region; // Create an OSS client. var client = new oss({ region: ossRegion, /* An Alibaba Cloud account AccessKey grants full API access. We recommend using a RAM user for API calls or routine O&M. Do not store your AccessKey ID and AccessKey secret in your code. This could lead to AccessKey leaks and compromise all resources under your account. This example retrieves AccessKeyId and AccessKeySecret from the context. */ accessKeyId: ctx.credentials.accessKeyId, accessKeySecret: ctx.credentials.accessKeySecret, stsToken: ctx.credentials.securityToken }); // Get the bucket name from the event. client.useBucket(ossEvent.oss.bucket.name); // Store processed images in the processed/ directory. var newKey = ossEvent.oss.object.key.replace("source/", "processed/"); var tmpFile = "/tmp/processed.png"; // Get the OSS object. console.log('Getting object: ', ossEvent.oss.object.key) client.get(ossEvent.oss.object.key).then(function (val) { // Read the OSS object content from the buffer. 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 a temporary file. image.resize(128, 128).write(tmpFile, function (err) { if (err) { console.error("Failed to write image locally"); callback(err); return; } // Upload the processed image to OSS with a new name. 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. Ensure the role attached to the function's service has OSS access permissions. Log on to the RAM console to grant OSS permissions to the role. # 2. Ensure the function handler 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 # An Alibaba Cloud account AccessKey grants full API access. We recommend using a RAM user for API calls or routine O&M. # Do not store your AccessKey ID and AccessKey secret in your code. This could lead to AccessKey leaks and compromise all resources under your account. # This example retrieves AccessKeyId and AccessKeySecret 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. Ensure the role attached to the function's service has OSS access permissions. Log on to the RAM console to grant OSS permissions to the role. 2. Ensure the function handler is index.handler. */ <?php use OSS\OssClient; function handler($event, $context) { $event = json_decode($event, $assoc = true); /* An Alibaba Cloud account AccessKey grants full API access. We recommend using a RAM user for API calls or routine O&M. Do not store your AccessKey ID and AccessKey secret in your code. This could lead to AccessKey leaks and compromise all resources under your account. This example retrieves AccessKey and AccessSecretKey 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()); } }Click the Test Function button on the Code tab.
After execution, view the results above the Code tab.
FAQ
References
To learn about configurable OSS event types, see OSS event definitions.
To modify or delete a trigger, see Update trigger configuration.
Trigger-related questions
To identify which event triggered a function execution, you can log the event type in your code. For more information, see Log records.
To call another function from within your function, you can use an API call or orchestrate functions with CloudFlow. For time-consuming asynchronous operations, you can configure an asynchronous invocation destination. For more information, see Can functions call each other? and Configure asynchronous invocation destinations.