前言
首先介紹下在本文出現的幾個比較重要的概念:
函數計算(Function Compute):函數計算是一個事件驅動的服務,通過函數計算,使用者無需管理伺服器等運作情況,隻需編寫代碼并上傳。函數計算準備計算資源,并以彈性伸縮的方式運作使用者代碼,而使用者隻需根據實際代碼運作所消耗的資源進行付費。函數計算更多資訊 參考。
Fun:Fun 是一個用于支援 Serverless 應用部署的工具,能幫助您便捷地管理函數計算、API 網關、日志服務等資源。它通過一個資源配置檔案(template.yml),協助您進行開發、建構、部署操作。Fun 的更多文檔
Ghostscript:Ghostscript 是一套建基于Adobe、PostScript及可移植文檔格式(PDF)的頁面描述語言等而編譯成的自由軟體。參見維基百科 詞條
備注: 本文介紹的技巧需要 Fun 版本大于等于 3.0.0-beta.7 。
依賴工具
本項目是在 MacOS 下開發的,涉及到的工具是平台無關的,對于 Linux 和 Windows 桌面系統應該也同樣适用。在開始本例之前請確定如下工具已經正确的安裝,更新到最新版本,并進行正确的配置。
Fun 工具依賴于 docker 來模拟本地環境。
對于 MacOS 使用者可以使用
homebrew進行安裝:
brew cask install docker
brew tap vangie/formula
brew install fun
Windows 和 Linux 使用者安裝請參考:
安裝好後,記得先執行
fun config
初始化一下配置。
注意, 如果你已經安裝過了 fun,確定 fun 的版本在 3.0.0-beta.7 以上。
$ fun --version
3.0.0-beta.7
初始化
使用 fun init 指令可以快捷地将本模闆項目初始化到本地。
fun init vangie/ghostscript-example
安裝依賴
$ fun install
Installing recursively on fun.yml
skip pulling image aliyunfc/runtime-python3.6:build-1.6.1...
Task => workaround for update-gsfontmap
=> bash -c 'mkdir -p /code/.fun/root/etc/ghostscript/cidfmap.d/ && mkdir -p /code/.fun/root/etc/ghostscript/fontmap.d/ && mkdir -p /etc/ghostscript/ && mkdir -p /var/lib/ghostscript/ && mkdir -p /code/.fun/root/var/lib/ghostscript/fonts && ln -s /code/.fun/root/etc/ghostscript/cidfmap.d /etc/ghostscript/ && ln -s /code/.fun/root/etc/ghostscript/fontmap.d /etc/ghostscript/ && ln -s /code/.fun/root/var/lib/ghostscript/fonts /var/lib/ghostscript/'
Task => [UNNAMED]
=> apt-get update (if need)
=> apt-get install -y -d -o=dir::cache=/code/.fun/tmp/install ghostscript --reinstall
=> bash -c
for f in $(ls /code/.fun/tmp/install/archives/*.deb); do
dpkg -x $f /code/.fun/root;
mkdir -p /code/.fun/tmp/install/deb-control/${f%.*};
dpkg -e $f /code/.fun/tmp/install/deb-control/${f%.*};
if [ -f "/code/.fun/tmp/install/deb-control/${f%.*}/postinst" ]; then
FUN_INSTALL_LOCAL=true /code/.fun/tmp/install/deb-control/${f%.*}/postinst configure;
fi;
done;
Creating config file /etc/papersize with new version
=> bash -c 'rm -rf /code/.fun/tmp/install/archives'
本地調用
$ fun local invoke pdf2jpg
using template: template.yml
skip pulling image aliyunfc/runtime-nodejs10:1.6.1...
FC Invoke Start RequestId: 21d9c646-1db4-403c-b018-cd4246e193d3
load code for handler:index.handler
2019-09-18T09:45:38.400Z 21d9c646-1db4-403c-b018-cd4246e193d3 [verbose] stdout =================== START
2019-09-18T09:45:38.400Z 21d9c646-1db4-403c-b018-cd4246e193d3 [verbose] GPL Ghostscript 9.26 (2018-11-20)
Copyright (C) 2018 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
2019-09-18T09:45:38.401Z 21d9c646-1db4-403c-b018-cd4246e193d3 [verbose] stdout =================== END
FC Invoke End RequestId: 21d9c646-1db4-403c-b018-cd4246e193d3
convert success.
JPG file save to /tmp/test.jpg
2019-09-18T09:45:38.416Z 21d9c646-1db4-403c-b018-cd4246e193d3 [error](node:21) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
RequestId: 21d9c646-1db4-403c-b018-cd4246e193d3 Billed Duration: 2132 ms Memory Size: 1998 MB Max Memory Used: 78 MB
可以檢視檔案 .fun/tmp/invoke/ghostscript/pdf2jpg/test.jpg ,預留轉換後的效果。
部署
$ fun deploy
using template: template.yml
using region: cn-shanghai
using accountId: ***********4733
using accessKeyId: ***********EUz3
using timeout: 60
Waiting for service ghostscript to be deployed...
Waiting for function pdf2jpg to be deployed...
Waiting for packaging function pdf2jpg code...
The function pdf2jpg has been packaged. A total of 1054 files files were compressed and the final size was 23.44 MB
function pdf2jpg deploy success
service ghostscript deploy success
執行
$ fun invoke pdf2jpg
fun invoke pdf2jpg
using template: template.yml
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 1411066b-1ad0-4750-922d-2350652ca5a6
load code for handler:index.handler
2019-09-18T09:52:39.802Z 1411066b-1ad0-4750-922d-2350652ca5a6 [verbose] stdout =================== START
2019-09-18T09:52:39.802Z 1411066b-1ad0-4750-922d-2350652ca5a6 [verbose] GPL Ghostscript 9.26 (2018-11-20)
Copyright (C) 2018 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
2019-09-18T09:52:39.802Z 1411066b-1ad0-4750-922d-2350652ca5a6 [verbose] stdout =================== END
FC Invoke End RequestId: 1411066b-1ad0-4750-922d-2350652ca5a6
Duration: 513.34 ms, Billed Duration: 600 ms, Memory Size: 128 MB, Max Memory Used: 56.49 MB
========= FC invoke Logs end =========
FC Invoke Result:
convert success.
JPG file save to /tmp/test.jpg