Deploy a flexible and highly available image processing service within 10 minutes
摘要： Alibaba Cloud Function Compute is an event-driven and fully-managed compute service. With Function Compute, you can quickly build any type of applicat.
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.
Take the classic
lena in image processing for example:
We use python runtime to complete this tutorial. Let's look at a few concepts before we get started.
def my_handler(event, context): return 'hello world'
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.
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.
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.
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.
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.request_uri, are added to Function Compute.
HTTP_Variablesfield of environ contains the request header. For example, if the request header is
'value', the environ parameter will be environ[
'value'. According to WSGI, the key in the request header is processed as follows:
key = "HTTP_" + k.upper().replace("-","_").
More details can be found at python handler
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:
For example, build a bucket named
Shenzhen(China south 1) region， where store the four pictures(you can casually name the bucket)
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:
image-proc, configurating function with a trigger named http-trigger.
AliyunOSSFullAccessand the authority of function execution log store into fc-log.
npm install git://github.com/aliyun/fun.git --save -g
fun deployhas been executed successfully, you may see the following resources being crested, screenshot:
Function compute has advantages as follows: