前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用腾讯云对象存储 COS 桶托管 hexo 博客

利用腾讯云对象存储 COS 桶托管 hexo 博客

作者头像
Cell
发布2022-02-25 16:11:09
3.2K0
发布2022-02-25 16:11:09
举报
文章被收录于专栏:Cell的前端专栏Cell的前端专栏

本以为 coding pages 与腾讯云合作后会更好,没想到正是这种初期 bug 不断,速度也是非常慢。比 gitee, 甚至 github 都要慢很多了。所以决定放弃 coding 了,本想挂到云服务器上,但是这个云服务器只续费了半年,可能不会再续费,前几天看到用腾讯云的 cos 桶 xml 制作动态相册的文章,知道了对象存储这个玩意,腾讯云 COS 提供免费 50G 的存储空间,还有 CDN 加速服务,我觉得是个不错的选择,部署后发现速度还挺好。 适用于 hexo, hugo 等静态博客的部署。

创建存储桶

打开腾讯云控制台–云产品–存储–对象存储,然后创建存储桶。

开启静态网站设置

在基础配置打开静态网站(关掉强制 https)

绑定域

SSL 设置

域名解析,添加记录

去 dns 服务商添加域名解析记录 CNAME 指向上面的域名

hexo 设置

  • 安装插件

1

npm install hexo-deployer-cos --save

  • 站点配置文件

1 2 3 4 5 6 7

deploy: type: cos bucket: yourBucketName #cos 桶名称 appId: yourAppId #cos 桶名称后数字 secretId: yourSecretId #云 API 密钥 secretKey: yourSecretKey #云 API 密钥 region: yourRegion #所属地域

  • 发布还是一样的

1 2

hexo clean hexo g -d

结果类似于

CDN 刷新

每次更新博客内容完后,都要登陆腾讯云 CDN–缓存刷新,手动刷新一下 CDN。

用脚本在每次更新后刷新

  • 安装

1

npm install qcloud-cdn-node-sdk --save

  • 创建qcloudcdn.js放入script文件夹

1 2 3 4 5 6 7 8 9 10 11 12

const qcloudSDK = require('qcloud-cdn-node-sdk'); qcloudSDK.config({ secretId: '你的 ID', secretKey: '你的密钥' }) qcloudSDK.request('RefreshCdnDir', { 'dirs.1': 'http://博客地址' }, (res) => { console.log(res) })

自动 CDN 刷新配置 (推荐)

  1. 进入腾讯云,找到 函数计算 -> CDN 缓存刷新函数 -> 创建 CDN 缓存刷新函数
  2. 修改 index.js 内容后重新部署

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

'use strict' const CosSdk = require('cos-nodejs-sdk-v5') const CdnSdk = require('./common/CdnSdk') const CdnRefreshTask = require('./common/CdnRefreshTask') const { getParams, getObjectUrl, logger, getLogSummary } = require('./common/utils') exports.main_handler = async (event, context, callback) => { /** * parse param from event and process.env */ const { objects, cdnHosts, secretId, secretKey, token } = getParams(event) logger({ title: 'param is parsed success, param as follow: ', data: { objects, cdnHosts, event } }) /** * init cos instance */ if (!secretId || !secretKey || !token) { throw new Error(`secretId, secretKey or token is missing`) } const cdnSdkInstance = new CdnSdk({ secretId, secretKey, token }) const cosInstance = new CosSdk({ SecretId: secretId, SecretKey: secretKey, XCosSecurityToken: token }) const taskList = objects.map(({ bucket, region, key }) => { /* 变更内容-START */ const purgeUrls = []; cdnHosts.forEach(host => { const tempUrl = getObjectUrl({ cosInstance, bucket, region, key, origin: `${/^(http\:\/\/|https\:\/\/)/.test(host) ? '' : 'https://'}${host}` }); purgeUrls.push(tempUrl); // 如果以 /index.html 结尾,则增加目录首页/。 // 例如 https://www.xxxx.com/index.html, 则增加 https://www.xxxx.com/。 if(tempUrl.lastIndexOf('/index.html') == (tempUrl.length - 11)){ purgeUrls.push(tempUrl.substr(0, tempUrl.length - 10)) } }); return new CdnRefreshTask({ cdnSdkInstance, urls: purgeUrls }) /* 变更内容-END */ }) const taskResults = [] for (const task of taskList) { const results = await task.runPurgeTasks() taskResults.push(...results) } logger({ title: 'cdn refresh full logs:', data: taskResults }) const { status, messages } = getLogSummary(taskResults) logger({ messages: messages.map(item => item.replace(/\,\ /g, '\n')) }) if (status === 'fail') { throw messages.join('; ') } else { return messages.join('; ') } }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建存储桶
  • 绑定域
  • 域名解析,添加记录
  • hexo 设置
  • CDN 刷新
  • 自动 CDN 刷新配置 (推荐)
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档