本文介紹fcli中函數相關的命令。
前提條件
在可執行檔所在檔案夾下執行fcli shell,進入互動模式。
建立函數(mkf)
-b string或--code-bucket string:指定代碼所在的OSS Bucket。-o string或--code-object string:指定代碼所在的Bucket中的Object Key。-d string或--code-dir string:指定代碼所在的目錄。-f string或--code-file string:指定壓縮的代碼檔案。-h string或--handler string:設定函數handler,handler的格式為“檔案名稱.函數名”。例如hello_world.handler指定了函數的調用入口為hello_world.js檔案中的handler函數。-e int32或--initializationTimeout int32:設定初始化函數逾時時間(預設30s)。-i string或--initializer string:設定初始化函數。-m int32或--memory int32:設定函數執行的記憶體大小。-t string或--runtime string:指定運行環境。--timeout int32:設定函數逾時時間(預設30s)。
// 在相應service目錄下
>>> mkf myFunction -t nodejs6 -h myFunction.handler -b ossBucketName -o objectKey //代碼儲存在OSS上,-t指定Runtime為Node.js6,-h指定函數入口,-b指定代碼所在的OSS Bucket,-o指定了代碼在Bucket中的Object Key。
>>> mkf myFunction -t nodejs6 -h myFunction.handler -d codeDir/myFunction -m 512 //代碼儲存在本地,-d指定了代碼所在目錄,-m設定函數執行的記憶體大小。
>>> mkf myFunction -h myFunction.handler -f code.zip -t nodejs6 //代碼在本地的code.zip中更新函數(upf)
-b string或--code-bucket string:指定代碼所在的OSS Bucket。-o string或--code-object string:指定代碼所在的Bucket中的Object Key。-d string或--code-dir string:指定代碼所在的目錄。-f string或--code-file string:指定壓縮的代碼檔案。-h string或--handler string:設定函數handler,handler的格式為“檔案名稱.函數名”。例如hello_world.handler指定了函數的調用入口為hello_world.js檔案中的handler函數。-m int32或--memory int32:設定函數執行的記憶體大小。-t string或--runtime string:指定運行環境。
// 在相應service目錄下
>>> upf myFunction -t nodejs6 -h myFunction.handler -b ossBucketName -o objectKey //代碼儲存在OSS上,-t指定Runtime為Node.js6,-b指定代碼所在的OSS Bucket,-o指定了代碼在Bucket中的Object Key。
>>> upf myFunction -t nodejs6 -h myFunction.handler -d codeDir/myFunction -m 512 //代碼儲存在本地,-d指定了代碼所在目錄,-m設定函數執行的記憶體大小。
>>> upf myFunction -h myFunction.handler -f code.zip -t nodejs6 //代碼在本地的code.zip中。
>>> upf myFunction -t nodejs6 -i myFunction.newInitializer -e 30 -b ossBucketName -o objectKey // 將initializer從myFunction.initializer更新至myFunction.newInitializer。代碼儲存在OSS上且包含initializer函數,-t指定Runtime,-i指定initializer入口,-e指定initializer逾時時間,-b指定代碼所在的OSS Bucket,-o指定了代碼在Bucket中的Object Key。
>>> upf myFunction -t nodejs6 -i "" -b ossBucketName -o objectKey // 將initializer從myFunction.newInitializer更新至空,即關閉函數initializer功能。
>>> upf myFunction -t nodejs6 -i myFunction.newInitializer -e 30 -b ossBucketName -o objectKey // 將initializer從空更新至myFunction.newInitializer。執行函數(invk)
-e或--encode:對函數的傳回值進行base64編碼。-f string或--event-file string:從檔案中讀取觸發事件內容。-s string或--event-str string:從字串中讀取觸發事件內容。-o string或--output-file string:將返回結果寫入檔案的檔案名稱。-t string或--type string:觸發類型,取值:- Sync:同步觸發(預設值)
- Async:非同步觸發
>>> invk myFunction //如果不需要輸入參數,不需要觸發事件的話,則直接調用。
>>> invk myFunction -e //對函數的傳回值進行base64編碼。
>>> invk myFunction -s 'hello,world'//從字串中讀取觸發事件內容。
>>> invk myFunction -f event.json //從檔案中讀取觸發事件內容。
>>> invk myFunction -o code/result.txt //將返回結果寫入result.txt檔案中。
>>> invk myFunction -t Async //設定觸發類型為非同步觸發。