腾讯云 COS 组件,可以快速部署静态网站页面到对象存储 COS 中,并生成域名供访问。

安装

首先要安装 serverless 组件

npm install -g serverless

在项目的根目录下生成配置文件

touch serverless.yml

<a name="YpT8f"></a>

配置

在 serverless.yml 文件中进行如下配置

# serverless.yml

myWebsite:
component: '@serverless/tencent-website'
inputs:
code:
src: build
hook: npm run build
index: index.html
error: index.html
region: ap-guangzhou
bucketName: my-bucket

部署

如您的账号未登陆或注册腾讯云,您可以直接通过 微信 扫描命令行中的二维码进行授权登陆和注册。

通过 sls 命令进行部署,并可以添加 --debug 参数查看部署过程中的信息(对于Windows用户来说, sls 命令可能无法使用,在使用时需要使用完整的 serverless 命令)

$ sls --debug

    DEBUG ─ Resolving the template's static variables.
DEBUG ─ Collecting components from the template.
DEBUG ─ Downloading any NPM components found in the template.
DEBUG ─ Analyzing the template's components dependencies.
DEBUG ─ Creating the template's components graph.
DEBUG ─ Syncing template state.
DEBUG ─ Executing the template's components graph.
DEBUG ─ Starting Website Component.
DEBUG ─ Preparing website Tencent COS bucket my-bucket-1300415943.
DEBUG ─ Deploying "my-bucket-1300415943" bucket in the "ap-guangzhou" region.
DEBUG ─ "my-bucket-1300415943" bucket was successfully deployed to the "ap-guangzhou" region.
DEBUG ─ Setting ACL for "my-bucket-1300415943" bucket in the "ap-guangzhou" region.
DEBUG ─ Ensuring no CORS are set for "my-bucket-1300415943" bucket in the "ap-guangzhou" region.
DEBUG ─ Ensuring no Tags are set for "my-bucket-1300415943" bucket in the "ap-guangzhou" region.
DEBUG ─ Configuring bucket my-bucket-1300415943 for website hosting.
DEBUG ─ Uploading website files from /Users/dfounderliu/Desktop/temp/code/src to bucket my-bucket-1300415943.
DEBUG ─ Starting upload to bucket my-bucket-1300415943 in region ap-guangzhou
DEBUG ─ Uploading directory /Users/dfounderliu/Desktop/temp/code/src to bucket my-bucket-1300415943
DEBUG ─ Website deployed successfully to URL: https://my-bucket-1300415943.cos-website.ap-guangzhou.myqcloud.com. myWebsite:
url: https://my-bucket-1300415943.cos-website.ap-guangzhou.myqcloud.com
env: 2s › myWebsite › done

移除

通过以下命令移除项目

$ sls remove --debug

  DEBUG ─ Flushing template state and removing all components.
DEBUG ─ Starting Website Removal.
DEBUG ─ Removing Website bucket.
DEBUG ─ Removing files from the "my-bucket-1300415943" bucket.
DEBUG ─ Removing "my-bucket-1300415943" bucket from the "ap-guangzhou" region.
DEBUG ─ "my-bucket-1300415943" bucket was successfully removed from the "ap-guangzhou" region.
DEBUG ─ Finished Website Removal. 3s › myWebsite › done

账号配置(可选)

当前默认支持 CLI 扫描二维码登录,如您希望配置持久的环境变量/秘钥信息,也可以本地创建 .env 文件

$ touch .env # 腾讯云的配置信息

在 .env 文件中配置腾讯云的 SecretId 和 SecretKey 信息并保存<br />

如果没有腾讯云账号,可以在此注册新账号。

如果已有腾讯云账号,可以在API 密钥管理中获取 SecretId 和SecretKey.

# .env
TENCENT_SECRET_ID=123
TENCENT_SECRET_KEY=123

配置文件详解

一个完整的配置文件如下所示

myWebsite:
component: '@serverless/tencent-website'
inputs:
code:
root: ./
src: ./src
hook: npm run build
index: index.html
error: index.html
region: ap-guangzhou
bucketName: my-bucket
protocol: http
hosts:
- host: abc.cn
- host: abc.com
fullUrl: on
cache:
- type: 0
rule: all
time: 1000
- type: 0
rule: all
time: 1000
cacheMode: simple
refer:
- type: 1
list:
- 'qq.baidu.com'
- '*.baidu.com'
accessIp:
type: 1
list:
- '1.2.3.4'
- '2.3.4.5'
https:
certId: 123
cert: 123
privateKey: 123
http2: off
httpsType: 2
forceSwitch: -2
env:
API_URL: https://api.com
cors:
- allowedOrigins:
- '*.tencent.com'
- '*.qcloud.com'
allowedMethods:
- PUT
- POST
- DELETE
- HEAD
allowedHeaders: '*'
maxAgeSeconds: 0
- allowedOrigins:
- '*'
allowedMethods:
- GET
allowedHeaders: '*'
maxAgeSeconds: 0

主要参数说明

参数 必须/可选 默认 描述
code 必须   网站代码信息
region 可选 ap-guangzhou 存储桶所属地域,默认为广州
bucketName 必须   存储桶名称,如果您不添加AppId后缀,则会自动为您添加,后缀不能大写
protocol 可选 https 协议:https 或者 http
env Optional   Environment variables to include in a 'env.js' file with your uploaded code.
cors Optional   Cross-Origin Resource Sharing
hosts Optional   Add domain

代码参数说明

参数 必须/可选 默认 描述
root 可选   您的网站项目的根文件夹。 默认为当前工作目录
src 必须   包含您构建的工件的要上传的文件夹
hook 可选   在上传之前对代码进行构建/测试/执行任何操作的钩子
index 可选 index.html 索引页
error 可选 error.html 错误页面_(对于使用前端框架构建的应用程序来说,需要将此参数设置为索引页,否则在页面刷新或重定向是会出现问题)_

来源:安徽网站优化

基于serverless快速部署前端项目到腾讯云的更多相关文章

  1. 部署Flask项目到腾讯云服务器CentOS7

    部署Flask项目到腾讯云服务器CentOS7 安装git yum install git 安装依赖包 支持SSL传输协议 解压功能 C语言解析XML文档的 安装gdbm数据库 实现自动补全功能 sq ...

  2. 基于云原生DevOps服务自动化部署前端项目学习总结

    本文主要以部署前端Vue项目为例,讲述了如何基于云原生DevOps服务自动化部署前端项目~从开发完成到线上环境,我们只需提交代码即可~ 一.引言 作为一名开发人员,日常工作中我们除了需要负责代码的开发 ...

  3. 使用vue2.x+webpack+vuex+sass+axios+elementUI等快速搭建前端项目框架

    一.本文将分享如何快速搭起基于webpack+vue的前端项目框架,利用vue的自己的脚手架工具vue-cli搭建起基本的环境配置,再通过npm包管理工具引入相应的依赖来完善项目的各种依赖框架.下面是 ...

  4. 记基于docker+gunicorn部署sanic项目遇到的很多很多坑

    前言: 最近有个项目需要上线,是python中sanic网络异步框架写的,并且要求使用docker+nginx来部署项目实现负载均衡,于是乎百度了sanic项目部署,基本上都是基于docker+gun ...

  5. docker自动化部署前端项目实战一

    docker自动化部署前端项目实战一 本文适用于个人项目,如博客.静态文档,不涉及后台数据交互,以部署文档为例. 思路 利用服务器node脚本,监听github仓库webhook push事件触发po ...

  6. 在基于ABP框架的前端项目Vue&Element项目中采用电子签名的处理

    在前面随笔介绍了<在基于ABP框架的前端项目Vue&Element项目中采用电子签章处理文件和打印处理>的处理,有的时候,我们在流程中或者一些文件签署的时候,需要签上自己的大名,一 ...

  7. Tomcat+Nginx+Linux+Mysql部署豆瓣TOP250的项目到腾讯云服务器

    写在前面 因为前面有写过一篇关于豆瓣的top250的电影的可视化展示项目,你可以移步http://blog.csdn.net/liuge36/article/details/78607955了解这个项 ...

  8. 在基于ABP框架的前端项目Vue&Element项目中采用电子签章处理文件和打印处理

    在一些内部OA或者流转的文件,或者给一些客户的报价文件.合同,或者一些医院出示的给保险机构的病历资料等,有时候可能都希望快速的使用电子签章的处理方式来给文件盖上特定的印章,本篇随笔介绍基于Vue&am ...

  9. 简单的利用nginx部署前端项目

    网上有很多教程写的一大堆东西,新手可能会有点看不懂,现在我写这篇文章是为了更好的帮助新手,如何将自己的前端项目部署到自己的服务器上. 首先我们必须要有一台自己的ubuntu服务器,如果没有可以去阿里云 ...

随机推荐

  1. jqGrid 添加 合计行 footDate

    jQuery(table_id).jqGrid({ url : url,//组件创建完成之后请求数据的url datatype : "json",//请求数据返回的类型.可选jso ...

  2. 5分钟搞懂:基于token的用户认证

    https://www.qikegu.com/easy-understanding/880 用户认证 用户认证或者说用户登录是确认某人确实是某人的过程,生活中靠身份证,网络上就要靠账号和密码.用户提供 ...

  3. AD中内电层设置

    用于走线与普铜 内电层分割

  4. 剑指offer_2.1_Day_5

    输入一个链表,按链表从尾到头的顺序返回一个ArrayList. import java.util.ArrayList; public class Solution { public ArrayList ...

  5. 干货分享|Law Essay写作高分攻略

    很多法学院的留学生对于Law Essay写作不是特别擅长,理论知识都了解,但是写出来的essay分数就是不高.同学们要从哪些方面入手呢?Law Essay写作要怎么拿高分?具体就跟小编一起来看看吧! ...

  6. MySQL数据类型使用总结,浮点使用注意事项

    1.对于精度要求较高的应用中,建议使用定点数来存储数值,以保证结果的准确性. 2.对于字符类型,要根据存储引擎进行相应的选择 3.对含有TEXT和BOLB字段的表,如果经常做删除和修改记录的操作要定时 ...

  7. Python调用c++可执行程序

    1.c++编译程序 #include <iostream> using namespace std; int test() { , b = ; return a+b; } int main ...

  8. Thread--CountDownLatch & CyclicBarrier

    参考:http://www.importnew.com/21889.html CountDownLatch countDown() 方法执行完只是计数器减一, 并不会阻塞当前运行线程的的后续代码执行. ...

  9. Python小数据池

    一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了,什么是内存地址呢? 你只要创建一个数据(对象)那么都 ...

  10. 系统学习python第七天学习笔记

    1.get方法用法补充 info = {'name':'王刚蛋','hobby':'铁锤'} while True: data = input('请输入:') val = info.get(data, ...