开发者社区> 阿里云Serverless Compute> 正文

开发函数计算的正确姿势——借助 LibreOffice 将 Word 转换成 PDF

简介: 前言首先介绍下在本文出现的几个比较重要的概念:函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。

前言

首先介绍下在本文出现的几个比较重要的概念:

函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息参考
Funcraft:Funcraft 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Funcraft 的更多文档参考
LibreOffice:LibreOffice 是由文档基金会开发的自由及开放源代码的办公室套件。LibreOffice套件包含文字处理器、电子表格、演示文稿程序、矢量图形编辑器和图表工具、数据库管理程序及创建和编辑数学公式的应用程序。参见维基百科词条

备注: 本文介绍的技巧需要 Funcraft 版本大于等于 3.0.6 。

依赖工具

本项目是在 MacOS 下开发的,涉及到的工具是平台无关的,对于 Linux 和 Windows 桌面系统应该也同样适用。在开始本例之前请确保如下工具已经正确的安装,更新到最新版本,并进行正确的配置。

Fun 工具依赖于 docker 来模拟本地环境。

对于 MacOS 用户可以使用 homebrew 进行安装:

brew cask install docker
brew tap vangie/formula
brew install fun

Windows 和 Linux 用户安装请参考:

  1. https://github.com/aliyun/fun/blob/master/docs/usage/installation.md

安装好后,记得先执行 fun config 初始化一下配置。

注意, 如果你已经安装过了 funcraft,确保 funcraft 的版本在 3.0.6 以上。

$ fun --version
3.0.6

初始化

使用 fun init 命令可以快捷地将本模板项目初始化到本地。

$ fun init vangie/libreoffice-example

修改 template.yml 文件,将 OSS_REGION 和 OSS_BUCKET 改为您账号的 OSS 配置。

该模板项目已经更新为使用 NAS 网盘存放 libreoffice 二进制文件。相比于 fc-libreoffice 项目中借助于 initializer + OSS 的方式缩短了初始化时间。对于 libreoffice 文件大小不再敏感,简化了未来升级 libreoffice 的难度,减少了做复杂的编译和裁剪工作。

安装依赖

$ fun install
using template: template.yml
start installing function dependencies without docker

building libreoffice/word2pdf-nodejs8
Funfile exist, Fun will use container to build forcely
Step 1/7 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs8:build-1.6.6
 ---> d38b47af4e9e
Step 2/7 : RUN fun-install apt-get install libnss3
 ---> Using cache
 ---> 71b4a0d6f676
Step 3/7 : RUN mkdir -p /mnt/auto/
 ---> Using cache
 ---> 3fbbfc5d7ef8
Step 4/7 : WORKDIR /mnt/auto
 ---> Using cache
 ---> f4b65a179d06
Step 5/7 : RUN curl https://fc-demo-public.oss-cn-hangzhou.aliyuncs.com/fun/examples/lo.tar.gz -o lo.tar.gz
 ---> Using cache
 ---> d1ba9fd1e188
Step 6/7 : RUN tar -xzf lo.tar.gz
 ---> Using cache
 ---> ab7011d1b370
Step 7/7 : RUN rm lo.tar.gz
 ---> Using cache
 ---> 34700b58a0e8
sha256:34700b58a0e8ab04898f8cae93f85ace5beb67fd7bb35e689395f9987101089a
Successfully built 34700b58a0e8
Successfully tagged fun-cache-cb5ac96f-00f8-4c05-b4d9-5e36874ee3b2:latest
copying function artifact to /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/nodejs8
copy from container /mnt/auto/. to localNasDir

build function using image: fun-cache-cb5ac96f-00f8-4c05-b4d9-5e36874ee3b2
running task flow NpmTaskFlow
running task: CopySource
running task: NpmInstall

building libreoffice/word2pdf-python3
Funfile exist, Fun will use container to build forcely
Step 1/7 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.6.6
 ---> 40ba07f7bda9
Step 2/7 : RUN fun-install apt-get install libnss3
 ---> Using cache
 ---> 4e4764b5c980
Step 3/7 : RUN mkdir -p /mnt/auto/
 ---> Using cache
 ---> 8009dc11bf7e
Step 4/7 : WORKDIR /mnt/auto
 ---> Using cache
 ---> 6e889ba3a676
Step 5/7 : RUN curl https://fc-demo-public.oss-cn-hangzhou.aliyuncs.com/fun/examples/lo.tar.gz -o lo.tar.gz
 ---> Using cache
 ---> 064cdb7fa85a
Step 6/7 : RUN tar -xzf lo.tar.gz
 ---> Using cache
 ---> 7030b6dde611
Step 7/7 : RUN rm lo.tar.gz
 ---> Using cache
 ---> 83f8fb18c2c9
sha256:83f8fb18c2c99b17ed5bbefcbc334a9a2e5955ac0257be00e88703ab21492052
Successfully built 83f8fb18c2c9
Successfully tagged fun-cache-6c286205-f96f-44f9-b27b-e2bfc4fb6f8e:latest
copying function artifact to /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/python3
copy from container /mnt/auto/. to localNasDir

Install Success


Tips for next step
======================
* Invoke Event Function: fun local invoke
* Invoke Http Function: fun local start
* Build Http Function: fun build
* Deploy Resources: fun deploy

同步文件到 NAS

同步本地 NAS 目录 .fun/nas/auto-default/libreoffice/ 下的文件到 NAS 网盘

$ fun nas sync
using template: template.yml

start fun nas init...
checking if _FUN_NAS_libreoffice needs to be deployed...
Waiting for service _FUN_NAS_libreoffice to be deployed...
        make sure role 'aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice' is exist
        role 'aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice' is already exist
        attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice
        attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice
        using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
                vpc already generated, vpcId is: vpc-j6c2caegv9uuavx2p4kdk
                vswitch already generated, vswitchId is: vsw-j6c6cqwh3x079caqc6xut
                security group already generated, security group is: sg-j6c8qsgt8wg0j99ospmt
        generated auto VpcConfig done:  {"vpcId":"vpc-j6c2caegv9uuavx2p4kdk","vswitchIds":["vsw-j6c6cqwh3x079caqc6xut"],"securityGroupId":"sg-j6c8qsgt8wg0j99ospmt"}
        using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
                nas file system already generated, fileSystemId is: 127fe949c0d
                nas file system mount target is already created, mountTargetDomain is: 127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com
        generated auto NasConfig done:  {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com:/libreoffice","MountDir":"/mnt/auto"}]}
        Checking if nas directories /libreoffice exists, if not, it will be created automatically
        Checking nas directories done ["/libreoffice"]
        Waiting for function fun-nas-function to be deployed...
                Waiting for packaging function fun-nas-function code...
                The function fun-nas-function has been packaged.
                Waiting for HTTP trigger httpTrigger to be deployed...
                methods: POST,GET
                url: https://1751705494334733.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/_FUN_NAS_libreoffice/fun-nas-function/
                function httpTrigger deploy success
        function fun-nas-function deploy success
service _FUN_NAS_libreoffice deploy success

Create local NAS directory of service libreoffice:
        /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/libreoffice
fun nas init Success

starting upload /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/libreoffice to nas://libreoffice/mnt/auto/
NAS path checking...
zipping /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/libreoffice
 /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/tmp/nas/sync/Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/.fun-nas-generated-libreoffice.zip - zipped
checking NAS tmp dir
 check done
Creating 124975363 bytes size file: /mnt/auto/.fun_nas_tmp/.fun-nas-generated-libreoffice.zip
 create done
 upload done
checking uploaded NAS zip file hash
 hash unchanged
unzipping file

 unzip done
cleaning
 clean done
 upload completed!

Tips for next step
======================
$ fun nas info      # Show NAS info
$ fun nas ls        # List NAS files
$ fun nas sync      # Synchronize files to nas
$ fun deploy        # Deploy Resources

部署

经过测试 libreoffice 在 MacOS 本地 Docker 环境里无法运行,可能和 Linux 的 kernel 版本有关,所以本例跳过了 fun local invoke ,直接 deploy 到云端运行。

$ fun deploy
using template: template.yml
using region: cn-hongkong
using accountId: ***********4733
using accessKeyId: ***********EUz3
using timeout: 600

Waiting for service libreoffice to be deployed...
        make sure role 'aliyunfcgeneratedrole-cn-hongkong-libreoffice' is exist
        role 'aliyunfcgeneratedrole-cn-hongkong-libreoffice' is already exist
        attaching policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
        attached policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
        attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
        attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
        using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
                vpc already generated, vpcId is: vpc-j6c2caegv9uuavx2p4kdk
                vswitch already generated, vswitchId is: vsw-j6c6cqwh3x079caqc6xut
                security group already generated, security group is: sg-j6c8qsgt8wg0j99ospmt
        generated auto VpcConfig done:  {"vpcId":"vpc-j6c2caegv9uuavx2p4kdk","vswitchIds":["vsw-j6c6cqwh3x079caqc6xut"],"securityGroupId":"sg-j6c8qsgt8wg0j99ospmt"}
        using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
                nas file system already generated, fileSystemId is: 127fe949c0d
                nas file system mount target is already created, mountTargetDomain is: 127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com
        generated auto NasConfig done:  {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com:/libreoffice","MountDir":"/mnt/auto"}]}
        Checking if nas directories /libreoffice exists, if not, it will be created automatically
        Checking nas directories done ["/libreoffice"]
        Waiting for function word2pdf-nodejs8 to be deployed...
                Waiting for packaging function word2pdf-nodejs8 code...
                The function word2pdf-nodejs8 has been packaged. A total of 29 files files were compressed and the final size was 1.63 MB
        function word2pdf-nodejs8 deploy success
        Waiting for function word2pdf-python3 to be deployed...
                Waiting for packaging function word2pdf-python3 code...
                The function word2pdf-python3 has been packaged. A total of 27 files files were compressed and the final size was 1.63 MB
        function word2pdf-python3 deploy success
service libreoffice deploy success

执行

调用 nodejs8 版本

$ fun invoke word2pdf-nodejs8
using template: template.yml
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 9f68ec8b-a1ca-4fab-a605-e5414a878c7e
load code for handler:index.handler
2019-10-14T08:20:52.559Z 9f68ec8b-a1ca-4fab-a605-e5414a878c7e [verbose] convert /tmp/example.docx -> /tmp/example.pdf using filter : writer_pdf_Export

FC Invoke End RequestId: 9f68ec8b-a1ca-4fab-a605-e5414a878c7e

Duration: 3397.91 ms, Billed Duration: 3400 ms, Memory Size: 640 MB, Max Memory Used: 203.46 MB
========= FC invoke Logs end =========

FC Invoke Result:
http://vangie-test.oss-cn-shanghai.aliyuncs.com/example.pdf

调用 python3 版本

$ fun invoke word2pdf-python3
using template: template.yml
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 4bbb507d-1f57-4ca1-a877-4ce4d9096d4d
convert /tmp/example.docx -> /tmp/example.pdf using filter : writer_pdf_Export
FC Invoke End RequestId: 4bbb507d-1f57-4ca1-a877-4ce4d9096d4d

Duration: 1855.37 ms, Billed Duration: 1900 ms, Memory Size: 640 MB, Max Memory Used: 67.22 MB
========= FC invoke Logs end =========

FC Invoke Result:
upload to oss success, the url is http://vangie-test.oss-cn-shanghai.aliyuncs.com/example.pdf?security-token=CAISmwJ1q6Ft5B2yfSjIr4%2BHJvjBgI9I%2F6CEM1D1oEMFf7dD16D61Tz2IHpFfnFsBukftvU3nW5U5%2FYYlqZdVplOWU3Da%2BB364xK7Q757QoRDHjwv9I%2Bk5SANTW5KXyShb3%2FAYjQSNfaZY3eCTTtnTNyxr3XbCirW0ffX7SClZ9gaKZ8PGD6F00kYu1bPQx%2FssQXGGLMPPK2SH7Qj3HXEVBjt3gX6wo9y9zmnZDFtUKD0AymkbRJ%2BN%2BqGPX%2BMZkwZqUYesyuwel7epDG1CNt8BVQ%2FM909vceqG2f4o7EWgEAu0zYb7uEqMcqJQt4d7U8FaVLof7xj%2FRkt%2BDJkID6jh1LeOFSVSvcQ4avhc%2FFEvmkMdg3dL32K8pAU1cDq3ieGoABAiBcEZ5ZzqlsXeIfiEKFAsIlNr3yYTflfBp%2FOr%2BktvB54GISQyX%2BzAlhBeq1IkBl3pudBcz%2FSsluxyR9kySjvx07UU4Zdh5dS%2BaNrDimZVvKxxYZaMtA%2FFqetiO1NZ6iE6GPOBe0lb5Hg%2FRHUrSaVK5JgQR%2B0JSvy%2BEUcw%2B44jE%3D&OSSAccessKeyId=STS.NL2mBumPiHbo1vDDCPs8o8eQ7&Expires=1571041909&Signature=kfDwm6JRZ2odtXU5IML3ETQjHds%3D

通过最后输出的 OSS 临时链接访问 pdf 文件,临时链接的有效期 10 分钟。

参考阅读

  1. 五分钟上线——函数计算 Word 转 PDF 云服务
  2. 手把手教您将 libreoffice 移植到函数计算平台
  3. https://github.com/awesome-fc/fc-libreoffice
  4. 模板项目
  5. Funcraft
  6. Aliyun Serverless VSCode Extension

版权声明:本文中所有内容均属于阿里云开发者社区所有,任何媒体、网站或个人未经阿里云开发者社区协议授权不得转载、链接、转贴或以其他方式复制发布/发表。申请授权请邮件developerteam@list.alibaba-inc.com,已获得阿里云开发者社区协议授权的媒体、网站,在转载使用时必须注明"稿件来源:阿里云开发者社区,原文作者姓名",违者本社区将依法追究责任。 如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
上一篇:手把手教您将 Ghostscript 移植到函数计算平台 下一篇:开发函数计算的正确姿势——借助 Ghostscript 将 PDF 转换成 JPG

Serverless微服务应用实践分享

函数计算官网