Guidelines for Function Compute Development -- OSS Trigger

本文涉及的产品
函数计算FC,每月15万CU 3个月
Serverless 应用引擎免费试用套餐包,4320000 CU,有效期3个月
简介: Preface Terms and descriptions: Function Compute: Alibaba Cloud Function Compute is an event-driven computing service that dynamically allocates resources after you write and upload code.

Preface

Terms and descriptions:

Function Compute: Alibaba Cloud Function Compute is an event-driven computing service that dynamically allocates resources after you write and upload code. After Function Compute allocates required computing resources, it elastically runs your code. Fees are calculated based on the actual resources consumed. For more information, see https://www.alibabacloud.com/help/product/50980.htm.

Fun: Fun is a developer tool for serverless applications. It helps you manage resources such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file. For more information, see https://github.com/aliyun/fun) .

The following example shows how Fun uses an Object Storage Service (OSS) trigger to associate OSS with Function Compute.

Event sources

image

Event sources are the collection of services that can invoke functions. Event sources include services such as OSS, Log Service, API Gateway, timers, and HTTP requests. When an event occurs, the corresponding event source determines whether to invoke functions. For example, after you create an OSS PutObject trigger, uploading an object to a specified OSS bucket is considered to be an event. The corresponding functions are then invoked. During this process, OSS is the event source. Function Compute supports a wide range of event sources to meet your requirements. For more information, see https://yq.aliyun.com/articles/679488.

Configure Fun and OSS trigger

A Function Compute trigger describes a set of rules. When an event matches the rules, the event source invokes the corresponding functions. Alibaba Cloud OSS is seamlessly integrated with Function Compute. You can configure functions for different types of events. When OSS detects a specified type of event, functions are automatically invoked.
You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file. You must describe an OSS trigger in the YML file by following the specifications. For example:

Events:
  oss-trigger-name:
    Type: OSS # trigger type
    Properties:
      BucketName: coco-superme # oss bucket name
      Events:
        - oss:ObjectCreated:*
        - oss:ObjectRemoved:DeleteObject        
      Filter: 
        Key:
          Prefix: source/
          Suffix: .png
  • oss-trigger-name: The name of the trigger. The name must be unique.
  • Type: Indicates the type of the event source. For more information about supported event sources, see https://github.com/aliyun/fun/blob/master/docs/specs/2018-04-03.md#OSS. In this example, the event type described in the YML file is OSS.
  • BucketName: The name of the OSS bucket. For more information about creating OSS buckets, see https://help.aliyun.com/document_detail/74762.html ).
  • Events: Indicates the type of the OSS event.
  • Filter: Filters parameters for OSS objects. Only objects that meet the filter conditions can invoke functions. The filter contains the following attributes:

    • Key: The filter can filter object keys and contains the following attributes:

      • Prefix: Matches prefixes.
      • Suffix: Matches suffixes.

If a file stored in OSS begins with source/ and ends with .png, then OSS automatically invokes functions to process the file when a specified event is detected. Sample code.

You can run the fun deploy command to create and deploy services:

using region: cn-shanghai
using accountId: ***********8320
using accessKeyId: ***********mTN4
using timeout: 10

Waiting for service oss-test-service to be deployed...
        Waiting for function oss-test-function to be deployed...
                Waiting for packaging function oss-test-function code...
                package function oss-test-function code done
                Waiting for OSS trigger oss-trigger-name to be deployed...
                function oss-trigger-name deploy success
        function oss-test-function deploy success
service oss-test-service deploy success

PS D:\fun\examples\oss-trigger>

You can view the OSS trigger that you have configured in the Function Compute console, as shown in the following figure:

image

The OSS trigger oss-trigger-name has been created.

Notes:

Configuration errors

The following log entry may appear when you deploy services:

image

This indicates that the name of the OSS trigger configured in the Function Compute console is inconsistent with the one described in the YML file. This means that the triggers configured in the Function Compute console are inconsistent with those you have described in the YML file. Fun will not modify the trigger configured in the console. You can delete the trigger in the console, or manually invoke functions when necessary.

Recursive calls

When you use OSS triggers, you must avoid recursive calls. For example, in a scenario where files are uploaded to an OSS bucket, the upload operation invokes a function. This function then generates one or multiple files that are written to the OSS bucket. The write operation invokes the function again, which leads to an execution loop.
To avoid costs incurred by recursive calls as described in the preceding example, we recommend that you add a prefix and suffix to the directories, respectively. For example, you can add the suffix src/ to the bucket directory and dst/ to the directly where files generated by the function are stored. This prevents the function from being invoked again by the generated files.

Summary

  • OSS triggers are described in the YML file. You must specify all attributes.
  • When OSS detects a specific event, the event information will be encrypted into a JSON string and passed to the corresponding function for processing. You can view the event information in the code. For more information, see https://www.alibabacloud.com/help/doc-detail/73331.htm .
  • Make sure that the configuration is correct and you can focus on writing the function logic. Function Compute can process large volumes of data in real time and in parallel.
相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
目录
相关文章
|
11月前
|
数据采集 Serverless API
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
321 2
|
2月前
|
存储 Serverless 数据库
Function Compute
【9月更文挑战第19天】
18 1
|
3月前
【Azure Function】Azure Function中的Timer Trigger无法自动触发问题
【Azure Function】Azure Function中的Timer Trigger无法自动触发问题
|
3月前
|
JSON 数据格式 Python
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
|
3月前
|
消息中间件 域名解析 网络协议
【Azure 应用服务】部署Kafka Trigger Function到Azure Function服务中,解决自定义域名解析难题
【Azure 应用服务】部署Kafka Trigger Function到Azure Function服务中,解决自定义域名解析难题
|
3月前
|
消息中间件 Kafka 网络安全
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验
|
6月前
|
运维 监控 JavaScript
【阿里云云原生专栏】Serverless架构下的应用部署与运维:阿里云Function Compute深度探索
【5月更文挑战第21天】阿里云Function Compute是事件驱动的无服务器计算服务,让用户无需关注基础设施,专注业务逻辑。本文详述了在FC上部署应用的步骤,包括创建函数、编写代码和部署,并介绍了运维功能:监控告警、日志管理、版本管理和授权管理,提供高效低成本的计算服务。
305 6
|
6月前
|
运维 监控 Dubbo
SAE(Serverless App Engine)和FC(Function Compute)
【1月更文挑战第18天】【1月更文挑战第89篇】SAE(Serverless App Engine)和FC(Function Compute)
185 1
|
6月前
|
存储 Serverless
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
307 2
|
监控 前端开发 Serverless
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
414 1

热门文章

最新文章