阿里云函数计算服务(FunctionCompute,FC)是一个事件驱动的全托管计算服务。经过函数计算与云端各个服务的普遍集成,开发者只须要编写函数代码,就可以快速地开发出弹性高可用的后端系统。接下来咱们使用FC,来快速实现一个图片转换服务, 并把这个图片转换服务做为支付宝小程序的后端。html
示例代码附件 【必须】前端
支付宝小程序开发工具下载 【非必须】node
函数计算FC 快捷入口
对象存储OSS 快捷入口
日志服务Log Service 快捷入口python
普通函数入口git
def my_handler(event, context): return 'hello world'
my_handler须要与建立函数时的"Handler"字段相对应:例如建立函数时指定的 Handler 为main.my_handler,那么函数计算会去加载main.py中定义的my_handler函数github
event 参数是用户调用函数时传入的数据,其类型是strnpm
context 参数中包含一些函数的运行时信息(例如 request id/临时 AK 等)。其类型是FCContext,具体结构和使用在下面的使用 context介绍小程序
函数的返回值会做为调用函数的结果返回给用户,它能够是任意类型:对于简单类型会函数计算会把它转换成 str 返回,对于复杂类型会把它转换成 JSON 字符串返回后端
HTTP 触发器的函数入口服务器
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 : environ 参数是一个 python 字典,里面存放了全部和客户端相关的信息,具体详情参考 environ 参数,函数计算增长了两个自定义的 key,分别是 fc.context
和 fc.request_uri
须要注意的点:environ 中的 HTTP_Variables
,里面包含 request 中 header, 好比某个请求的 header 的为 'x-Custom-key':'value'
, 在 environ 中会表现为:environ['HTTP_X_CUSTOM_KEY']='value'
, 能够理解为,对于 request header 中的 key,WSGI 作以下处理:key = "HTTP_" + k.upper().replace("-","_")
更多详细介绍请参考函数入口和python runtime
假定此次实验全部操做在华东2 上海region 完成,全部实验相关的资源请从附件中下载
这里介绍两种部署函数的方法:
xcx-demo
的bucketFun 是用于在阿里云上定义 serverless 应用的模型。
Serverless 应用是由事件触发功能组成的应用。一个典型的 serverless 应用由一个或多个由诸如向 阿里云 OSS 上传对象,在 阿里云 OTS 上执行数据操做以及 API 操做等事件触发的阿里云函数计算组成。这些函数能够独立使用。也能够利用其它资源,例如阿里云 OTS 的表和 OSS 的 buckets。最基本的 serverless 应用能够只有一个函数。
好比对于这个案例,定义的yaml文件以下:
ROSTemplateFormatVersion: '2015-09-01' Transform: 'Aliyun::Serverless-2018-04-03' Resources: sh-pro: Type: 'Aliyun::Serverless::Log' Properties: Description: 'image process log pro' fc-log: Type: 'Aliyun::Serverless::Log::Logstore' Properties: TTL: 362 ShardCount: 1 pydemo: Type: 'Aliyun::Serverless::Service' Properties: Description: 'fc xiaochengxu demo' Policies: - AliyunOSSFullAccess LogConfig: Project: 'sh-pro' Logstore: 'fc-log' upload: Type: 'Aliyun::Serverless::Function' Properties: Handler: upload.handler CodeUri: './' Description: 'http function for upload image' Runtime: python2.7 Timeout: 60 MemorySize: 256 Events: http-trigger: Type: HTTP Properties: AuthType: ANONYMOUS Methods: ['POST'] proc: Type: 'Aliyun::Serverless::Function' Properties: Handler: proc.handler CodeUri: './' Description: 'http function for process image' Runtime: python2.7 Timeout: 60 MemorySize: 256 Events: http-trigger: Type: HTTP Properties: AuthType: ANONYMOUS Methods: ['GET']
上面的定义的yaml文件要作如下几件事情:
sh-pro
, logstore: fc-log
xcxdemo
以及function: upload
和 proc
, 函数添加了一个类型为HTTP
,名叫http-trigger
的triggerAliyunOSSFullAccess
和函数执行日志写到fc-log的权限具体操做
npm install @alicloud/fun -g
fun deploy
执行成功后,应该能够看到以下资源被建立, 截图:
建立函数,而且配置http trigger
新建service,配置service一个具备访问oss权限的role
code.zip
curl https://1186202104331798.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/pydemo/upload/ -F "lena.png=@/Users/songluo/work/shworkshop/wp/lena.png" -v curl https://1186202104331798.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/pydemo/proc/rotate/10/lena.png >> 1.png
而后打开 调试器 和 模拟器
函数计算有以下优点:
本文为云栖社区原创内容,未经容许不得转载。