开发函数计算的正确姿势——网页截图服务

本文涉及的产品
函数计算FC,每月15万CU 3个月
Serverless 应用引擎免费试用套餐包,4320000 CU,有效期3个月
简介: 前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。

前言

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

函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息参考
Fun: Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Fun 的更多文档参考


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

依赖工具

本项目是在 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
  2. https://github.com/aliyun/fcli/releases

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

初始化

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

fun init vangie/puppeteer-example

安装依赖

fun install

fun install 会执行 Funfile 文件里的指令,依次执行如下任务:

  1. 安装 chrome headless 二进制文件;
  2. 安装 puppeteer 依赖的 apt 包;
  3. 安装 npm 依赖。

部署

同步大文件到 nas 盘

fun nas sync

部署代码

$ fun deploy
using template: template.yml
using region: cn-hangzhou
using accountId: ***********3743
using accessKeyId: ***********Ptgk
using timeout: 600

Waiting for service puppeteer to be deployed...
        make sure role 'aliyunfcgeneratedrole-cn-hangzhou-puppeteer' is exist
        role 'aliyunfcgeneratedrole-cn-hangzhou-puppeteer' is already exist
        attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hangzhou-puppeteer
        attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hangzhou-puppeteer
        using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
                vpc already generated, vpcId is: vpc-bp1wv9al02opqahkizmvr
                vswitch already generated, vswitchId is: vsw-bp1kablus0jrcdeth8v35
                security group already generated, security group is: sg-bp1h2swzeb5vgjfu6gpo
        generated auto VpcConfig done:  {"vpcId":"vpc-bp1wv9al02opqahkizmvr","vswitchIds":["vsw-bp1kablus0jrcdeth8v35"],"securityGroupId":"sg-bp1h2swzeb5vgjfu6gpo"}
        using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
                nas file system already generated, fileSystemId is: 0825a4a395
                nas file system mount target is already created, mountTargetDomain is: 0825a4a395-rrf16.cn-hangzhou.nas.aliyuncs.com
        generated auto NasConfig done:  {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"0825a4a395-rrf16.cn-hangzhou.nas.aliyuncs.com:/puppeteer","MountDir":"/mnt/auto"}]}
        Checking if nas directories /puppeteer exists, if not, it will be created automatically
        Checking nas directories done ["/puppeteer"]
        Waiting for function html2png to be deployed...
                Waiting for packaging function html2png code...
                The function html2png has been packaged. A total of 7 files files were compressed and the final size was 2.56 KB
                Waiting for HTTP trigger httpTrigger to be deployed...
                triggerName: httpTrigger
                methods: [ 'GET' ]
                url: https://xxxxxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/puppeteer/html2png/
                Http Trigger will forcefully add a 'Content-Disposition: attachment' field to the response header, which cannot be overwritten 
                and will cause the response to be downloaded as an attachment in the browser. This issue can be avoided by using CustomDomain.

                trigger httpTrigger deploy success
        function html2png deploy success
service puppeteer deploy success


===================================== Tips for nas resources ==================================================
Fun has detected the .nas.yml file in your working directory, which contains the local directory:

        /Users/vangie/Workspace/puppeteer-example/{{ projectName }}/.fun/root
        /Users/vangie/Workspace/puppeteer-example/{{ projectName }}/node_modules
  
The above directories will be automatically ignored when 'fun deploy'.
Any content of the above directories changes,you need to use 'fun nas sync' to sync local resources to remote.
===============================================================================================================

验证

curl https://xxxxxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/puppeteer/html2png/ > screenshot.png

如果不传递查询参数,默认会截取阿里云的首页

如果想换一个网址,可以使用如下命令格式

curl https://xxxxxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/puppeteer/html2png/?url=http://www.alibaba.com > screenshot.png

调试

如果需要在本地调试代码,可以使用如下命令

$ fun local start
using template: template.yml
HttpTrigger httpTrigger of puppeteer/html2png was registered
        url: http://localhost:8000/2016-08-15/proxy/puppeteer/html2png
        methods: [ 'GET' ]
        authType: ANONYMOUS


function compute app listening on port 8000!

浏览器打开 http://localhost:8000/2016-08-15/proxy/puppeteer/html2png 即可。

参考阅读

  1. 三分钟学会如何在函数计算中使用 puppeteer
相关实践学习
【AI破次元壁合照】少年白马醉春风,函数计算一键部署AI绘画平台
本次实验基于阿里云函数计算产品能力开发AI绘画平台,可让您实现“破次元壁”与角色合照,为角色换背景效果,用AI绘图技术绘出属于自己的少年江湖。
从 0 入门函数计算
在函数计算的架构中,开发者只需要编写业务代码,并监控业务运行情况就可以了。这将开发者从繁重的运维工作中解放出来,将精力投入到更有意义的开发任务上。
目录
相关文章
|
安全 网络安全 API
Hotmail邮箱设置Exchange的方法步骤
如何使用Exchange协议设置Hotmail邮箱:在邮件客户端(如Outlook)中添加新账户,选择Exchange服务,输入邮箱全址(@hotmail.com)和服务器地址(outlook.office365.com),启用SSL加密,完成设置后即可在各设备同步邮件、日历和联系人。AokSend提供触发式验证码发信服务,支持SMTP/API接口,高触达,独立IP和服务器。
|
测试技术 Linux 调度
你必须知道的Docker资源限制
本文探索了Docker的资源限制相关知识,在日常开发中应该给容器设置一个合理的资源限制值,以防出现OOME的情况导致Linux杀掉错误的进程。
2675 0
|
Shell 网络安全 开发工具
Git,GitHub,Gitee&IDEA集成Git
Git提交项目到GitHub简洁版、版本控制、安装、常用命令、分支、团队协作机制、Github、Gitee远程仓库、IDEA集成Git、IDEA集成Github、IDEA集成Gitee
Git,GitHub,Gitee&IDEA集成Git
|
编解码 安全 数据安全/隐私保护
【教程】苹果上架常见要求的有哪些?
【教程】苹果上架常见要求的有哪些?
|
存储 JSON 前端开发
【第40期】一文彻底搞懂React网络请求
【第40期】一文彻底搞懂React网络请求
520 0
|
Java 应用服务中间件
完美解决tomcat启动异常:Invalid byte tag in constant pool: 19;Unable to process Jar entry [module-info.class]
完美解决tomcat启动异常:Invalid byte tag in constant pool: 19;Unable to process Jar entry [module-info.class]
1910 0
|
移动开发 小程序 Android开发
uniapp使用webview将页面转换成图片支持h5、app、小程序
uniapp使用webview将页面转换成图片支持h5、app、小程序
|
Web App开发 前端开发 JavaScript
前端基本开发工具的浏览器之Safari
Safari是一款苹果公司开发的浏览器,它在Mac OS和iOS操作系统上广受欢迎。下面就让我们来看看Safari的特点和优势。
614 0
|
存储 安全 数据安全/隐私保护
iOS APP打包上传到APPstore的最新步骤​
iOS APP打包上传到APPstore的最新步骤​
|
弹性计算 缓存 网络协议
2023阿里云通用算力型u1云服务器配置性能评测及价格参考
阿里云通用算力型u1云服务器配置性能评测及价格参考2023年更新,CPU采用2.5 GHz主频的Intel(R) Xeon(R) Platinum处理器,通用算力型u1云服务器不适用于游戏和高频交易等需要极致性能的应用场景及对业务性能一致性有强诉求的应用场景(比如业务HA场景主备机需要性能一致)
759 0

热门文章

最新文章

相关产品

  • 函数计算