首先介紹下在本文出現的幾個比較重要的概念:html
函數計算(Function Compute):函數計算是一個事件驅動的服務,經過函數計算,用戶無需管理服務器等運行狀況,只需編寫代碼並上傳。函數計算準備計算資源,並以彈性伸縮的方式運行用戶代碼,而用戶只需根據實際代碼運行所消耗的資源進行付費。函數計算更多信息 參考。
Fun:Fun 是一個用於支持 Serverless 應用部署的工具,能幫助您便捷地管理函數計算、API 網關、日誌服務等資源。它經過一個資源配置文件(template.yml),協助您進行開發、構建、部署操做。Fun 的更多文檔 參考。
Ghostscript:Ghostscript 是一套建基於Adobe、PostScript及可移植文檔格式(PDF)的頁面描述語言等而編譯成的自由軟件。參見維基百科 詞條
備註: 本文介紹的技巧須要 Fun 版本大於等於 3.0.0-beta.7 。node
本項目是在 MacOS 下開發的,涉及到的工具是平臺無關的,對於 Linux 和 Windows 桌面系統應該也一樣適用。在開始本例以前請確保以下工具已經正確的安裝,更新到最新版本,並進行正確的配置。python
Fun 工具依賴於 docker 來模擬本地環境。git
對於 MacOS 用戶能夠使用 homebrew 進行安裝:github
brew cask install docker brew tap vangie/formula brew install fun
Windows 和 Linux 用戶安裝請參考:docker
安裝好後,記得先執行 fun config
初始化一下配置。bash
注意, 若是你已經安裝過了 fun,確保 fun 的版本在 3.0.0-beta.7 以上。服務器
$ fun --version 3.0.0-beta.7
使用 fun init 命令能夠快捷地將本模板項目初始化到本地。less
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
本文爲雲棲社區原創內容,未經容許不得轉載。