前言
首先介绍下在本文出现的几个比较重要的概念:
函数计算(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