【笔记】利用七牛云CDN加速提高前端图片静态资源访问速率

第一步,注册七牛云账号

打开 七牛开发者平台,选择 注册,注册后登录系统然后实名认证。

第二步,创建七牛云空间

七牛对象存储将数据文件以资源的形式上传到空间中。您可以创建一个或者多个空间,然后向每个空间中上传一个或多个文件。通过获取已上传文件的地址进行文件的分享和下载。您还可以通过修改存储空间或文件的属性或元信息来设置相应的访问权限。

静态资源要放到七牛云空间,一般有下面几个步骤的操作:

  • Step 1:创建空间
  • Step 2:上传文件
  • Step 3:下载文件
  • Step 4:删除文件
  • Step 5:删除空间

详细操作见官网:《快速入门文档》

第三步,添加加速域名,验证域名归属权,配置CNAME

享受七牛云CDN为您提供的加速服务时,需要先进行域名接入。

有两步:

  • 添加域名,验证域名归属权
  • 配置CNAME

注:在配置域名的时候,需要先创建空间,取一个空间名称。

添加域名参考:《域名接入》

域名归属校验参考:《域名归属校验》

CNAME配置参考:《CNAME配置》

第四步,本地资源文件同步到七牛云空间

图形化工具 Kodo Browser

如果需要用图形化工具详细参考官网:《图形化工具 Kodo Browser》

命令行工具qshell

工具的下载与安装

其中该工具的下载安装等参考文档:《命令行工具(qshell)》

下面以window平台为例,其中工具的下载参考官网的链接进行下载。

(1)闪退问题

本工具是一个命令行工具,在Windows下面请先打开命令行终端,然后输入工具名称执行,不要双击打开,否则会出现闪退现象。

(2)任何位置运行

如果你希望可以在任意目录下使用qshell,请将qshell工具可执行文件所在目录添加到系统的环境变量中。由于Windows系统是图形界面,所以方便一点。假设qshell.exe命令被解压到路径E:\jemy\tools目录下面,那么我们把这个目录放到系统的环境变量PATH里面。

(3)文本编码问题

当使用qupload等需要配置文件的命令时,不要使用Notepad++等编辑器来编写配置文件,在本地右键默认新建一个文本文档来进行编写。

(4) Windows 平台文件路径和字符

  • Windows 平台下的文件路径需要写为\\的写法,如C:\\Users\\li\\Downloads
  • 在使用命令和配置文件时,需要使用“”双引号,不能使用单引号。

密钥设置

该工具有两类命令,一类需要鉴权,另一类不需要。

需要鉴权的命令都需要依赖七牛账号下的AccessKeySecretKey。所以这类命令运行之前,需要使用 account 命令来设置下 AccessKeySecretKey

$ qshell account ak sk name

其中:

  • ak:AccessKey
  • sk:SecretKey
  • name:账号名称(例如手机号)

同步数据到七牛云空间

同步数据到七牛云空间有两种方式:

  • 配置式
  • 命令式

配置式的使用qupload。详细使用参考:https://github.com/qiniu/qshell/blob/master/docs/qupload.md

命令式的使用qupload2,详细使用参考:https://github.com/qiniu/qshell/blob/master/docs/qupload2.md

命令:

qshell qupload2 --overwrite=true --src-dir=/home/jemy/temp --bucket=test

以window为例,例如本地资源文件在D盘文件夹:

qshell qupload2 --overwrite=true --src-dir=D:\\vue\\cdnres --bucket=test2

其中的test2为七牛云的空间名称。

注:如果本地没有的资源,空间有的,不会删除空间有的资源。

Batch upload files to the qiniu bucket

Usage:
  qshell qupload2 [flags]

Flags:
      --bucket string                    bucket 同步数据的目标空间名称,可以为公开空间或私有空间。
  -T, --callback-host string             upload callback host
  -l, --callback-urls string             upload callback urls, separated by comma
      --check-exists                     check file key whether in bucket before upload 每个文件上传之前是否检查空间中是否存在同名文件,默认为 false(检查文件是否在空间中存在)。
      --check-hash                       check hash 在 check_exists 设置为 true 的情况下生效,是否检查本地文件 hash 和空间文件 hash 一致;默认为 false(不检查 hash),节约同步时间。
      --check-size                       check file size 在 check_exists 设置为 true 的情况下生效,是否检查本地大小和空间文件大小一致,优先级低于 check_hash;检查耗时小于 check_hash;默认为 false(检查文件大小是否一致)。
      --failure-list string              upload failure file list
      --file-list string                 file list to upload
      --file-type int                    set storage type of file, 0:STANDARD storage, 1:IA storage, 2:ARCHIVE storage, 3:DEEP_ARCHIVE storage
  -h, --help                             help for qupload2
      --ignore-dir                       ignore the dir in the dest file key
      --key-prefix string                key prefix prepended to dest file key
      --log-file string                  log file
      --log-level string                 log level (default "debug")
      --log-rotate int                   log rotate days (default 7)
      --overwrite                        overwrite the file of same key in bucket 是否覆盖空间中已有的同名文件,默认为 false(不覆盖)
      --overwrite-list string            upload success (overwrite) file list
      --put-threshold int                chunk upload threshold, unit: B (default 8388608)
      --record-root string               record root dir, and will save record info to the dir(db and log), default <UserRoot>/.qshell
      --rescan-local                     rescan local dir to upload newly add files
      --resumable-api-v2                 use resumable upload v2 APIs to upload
      --resumable-api-v2-part-size int   the part size when use resumable upload v2 APIs to upload (default 4194304)
      --skip-file-prefixes string        skip files with these file prefixes
      --skip-fixed-strings string        skip files with the fixed string in the name
      --skip-path-prefixes string        skip files with these relative path prefixes
      --skip-suffixes string             skip files with these suffixes
      --src-dir string                   src dir to upload
  -s, --storage int                      set storage type of file, 0:STANDARD storage, 1:IA storage, 2:ARCHIVE storage, 3:DEEP_ARCHIVE storage
      --success-list string              upload success file list
      --thread-count int                 multiple thread count (default 1)
      --worker-count int                 the number of concurrently uploaded parts of a single file in resumable upload (default 3)
      --up-host string                   upload host
posted @ 2023-01-29 20:12  风雨后见彩虹  阅读(713)  评论(0编辑  收藏  举报