如何使用云函數(shù)?
當(dāng)你對(duì)存儲(chǔ)在SyncStore的數(shù)據(jù)發(fā)生變化的時(shí)候“感興趣”,我們提供了基于事件觸發(fā)的云端函數(shù)計(jì)算服務(wù) —— YunFunction。你只要在感興趣的數(shù)據(jù)上【創(chuàng)建觸發(fā)器】,使得數(shù)據(jù)變化時(shí),系統(tǒng)自動(dòng)調(diào)用關(guān)聯(lián)的YunFunction;那么,當(dāng)函數(shù)被調(diào)用時(shí),相應(yīng)數(shù)據(jù)會(huì)透?jìng)鹘o函數(shù),參與函數(shù)計(jì)算邏輯。
主要操作步驟見下:
創(chuàng)建函數(shù)
說(shuō)明
當(dāng)前提供了函數(shù)包的方式進(jìn)行函數(shù)的創(chuàng)建,函數(shù)的運(yùn)行環(huán)境(Runtime)當(dāng)前僅支持NodeJS(4.4+)。函數(shù)包是以.zip為后綴的、標(biāo)準(zhǔn)的npm包。
函數(shù)包創(chuàng)建
>mkdir yunfunction_demo
>cd yunfunction_demo
>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (yunfunction_demo)
version: (1.0.0)
description: This is a yunfunction demo
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to yunfunction_demo/package.json:
{
"name": "yunfunction_demo",
"version": "1.0.0",
"description": "This is a yunfunction demo",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this ok? (yes) yes
TODO 編寫代碼邏輯...
>zip -q -r yunfunction_demo.zip *
上傳包文件
進(jìn)入代碼管理頁(yè)面,選擇本地準(zhǔn)備好的函數(shù)包文件并上傳。
部署函數(shù)
進(jìn)入代碼管理頁(yè)面,在已上傳的函數(shù)包記錄中,點(diǎn)擊部署,在部署函數(shù)表單中選擇部署環(huán)境,點(diǎn)擊確定。
運(yùn)行(調(diào)試)函數(shù)
進(jìn)入代碼管理頁(yè)面,在已上傳的函數(shù)包記錄中,點(diǎn)擊運(yùn)行,在運(yùn)行表單中選擇目標(biāo)函數(shù)、運(yùn)行環(huán)境及函數(shù)入?yún)ⅲc(diǎn)擊確定。
注:入?yún)鬟f給函數(shù)的第一個(gè)參數(shù)(如:event)
查看運(yùn)行日志
進(jìn)入日志查詢頁(yè)面,選擇目標(biāo)函數(shù)及環(huán)境,點(diǎn)擊搜索。
創(chuàng)建觸發(fā)器
進(jìn)入觸發(fā)器頁(yè)面,選擇namespace,點(diǎn)擊創(chuàng)建觸發(fā)器,配置觸發(fā)器關(guān)聯(lián)的觸發(fā)源、運(yùn)行環(huán)境、選擇目標(biāo)函數(shù)及函數(shù)調(diào)用模式,點(diǎn)擊確定。