Guidelines for Function Compute Development -- OSS Trigger

本文涉及的产品
简介: 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,搭建一个在线教育视频课程分享网站。
目录
相关文章
|
4月前
|
数据采集 Serverless API
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
284 2
|
3月前
|
运维 监控 Dubbo
SAE(Serverless App Engine)和FC(Function Compute)
【1月更文挑战第18天】【1月更文挑战第89篇】SAE(Serverless App Engine)和FC(Function Compute)
88 1
|
4月前
|
存储 Serverless
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
238 2
|
5月前
|
监控 前端开发 Serverless
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
323 1
|
8月前
|
运维 JavaScript Serverless
Function Compute
函数计算(Function Compute)是云计算领域的一种服务模型,由云服务提供商(例如阿里云、AWS、Google Cloud 等)提供。它是一种无服务器计算服务,允许开发者编写和部署函数,以响应事件触发,而无需管理底层的服务器和基础设施。函数计算提供了弹性的计算资源分配、按需计费、自动扩缩容等特性,使开发者能够聚焦于编写函数逻辑而不必担心底层的运维工作。
199 2
|
9月前
|
Serverless
函数计算(Function Compute)部署失败可能有多种原因
函数计算(Function Compute)部署失败可能有多种原因
114 2
|
10月前
|
弹性计算 监控 负载均衡
阿里云函数计算(Function Compute):快速高效的事件驱动计算
阿里云函数计算(Function Compute)是一种事件驱动计算服务,能够在阿里云上运行代码,且只按照实际使用时间付费。它无需管理服务器和基础架构,并可以与其他阿里云产品以及第三方服务集成,为用户提供了快速、高效、低成本、弹性的云计算能力。
|
数据采集 消息中间件 监控
Function Compute构建高弹性大数据采集系统
解决问题: 1.利用服务器自建数据采集系统成本高,弹性不足。 2.利用服务器自建数据采集系统运维复杂,成本高。
Function Compute构建高弹性大数据采集系统
|
SQL 数据库 索引
Using Python to Connect Function Compute to SQL Server
Normally, a third-party module is required for establishing Python-based database connections. To connect to Microsoft SQL Server, pymssql is required.
12596 0
|
JavaScript Java Linux
Installing a Dependency Library for Function Compute
In common programming practice, projects, libraries, and system environments must be installed and configured in synergy.
14054 0

热门文章

最新文章