Featured image of post 博客照片附件批量迁移cloudflareR2存储

博客照片附件批量迁移cloudflareR2存储

AI 摘要

前言

上文《使用cloudflare的R2创建免费图床》为了无人机拍摄的照片有个好去,利用cloudflareR2搭建图床。今天继续将博客文章中的资源(图片、附件、视频等)通过picgo批量迁移到图床上,以减轻git版本仓库体积,这里分享下我的实践。

前言

上文《使用cloudflare的R2创建免费图床》为了无人机拍摄的照片有个好去,利用cloudflareR2搭建图床。今天继续将博客文章中的资源(图片、附件、视频等)通过picgo批量迁移到图床上,以减轻git版本仓库体积,这里分享下我的实践。

正文

博客文章结构如下,准备将文章同级目录下的图片、附件、视频按原结果上传到图床,然后再修改文章(index.md)内容中的引用方式,将![1.png](1.png)改成![1.png](https://image.vitshare.cn/blog/content/post/demo/1.png),最后清理本地附件,精简git仓库。

├── content
│   └── post
│       ├── 分享windows平板屏幕电量监控程序,python全套源码开放
│       │   ├── 1.png
│       │   ├── 2.png
│       │   ├── 源码.zip
│       │   └── index.md
│       ├── 《蛤蟆先生去看心理医生》读后感
│       │   ├── 1.jpeg
│       │   └── index.md
│       └── 获取《王者荣耀》全英雄高清无码图
│           ├── 20180607141717128.png
│           ├── 20180607141729211.png
│           ├── 20180607144357253.png
│           └── index.md

修改picgo命令行部分代码

查阅picgo参数,发现命令行版本无法修改云端路径,所以我通过环境变量和setconfig方法曲线救国,具体修改如下:

#!/usr/bin/env node
const path = require('path')
const minimist = require('minimist')
const argv = minimist(process.argv.slice(2))
let configPath = argv.c || argv.config || ''
if (configPath !== true && configPath !== '') {
  configPath = path.resolve(configPath)
} else {
  configPath = ''
}
const { PicGo } = require('..')
const picgo = new PicGo(configPath)
picgo.registerCommands()

/* 新增部分 */
let uploadPath = process.env.uploadPath
picgo.log.info(uploadPath)
if ( uploadPath !==''){ 
  const current = picgo.getConfig('picBed.current')
  picgo.setConfig({ 'picBed.aws-s3.uploadPath' : uploadPath })
}
/* 新增部分 end*/

try {
  picgo.cmd.program.parse(process.argv)
} catch (e) {
  picgo.log.error(e)
  if (process.argv.includes('--debug')) {
    Promise.reject(e)
  }
}

批量上传脚本

find . -iname "*.jpg" >> 1.sh
find . -iname "*.jpeg" >> 1.sh
find . -iname "*.png" >> 1.sh
find . -iname "*.webp" >> 1.sh
find . -iname "*.webm" >> 1.sh
find . -iname "*.rar" >> 1.sh
find . -iname "*.zip" >> 1.sh
find . -iname "*.mp4" >> 1.sh
find . -iname "*.mov" >> 1.sh

#组织picgo上传指令
vim 1.sh 
:%s/\(^.*\)$/uploadPath=\1 picgo -c ~\/.config\/picgo\/data.json upload \1/g
:%s/^uploadPath=\/home\/zfj\/workspace\//uploadPath=/g

最后1.sh中内容

uploadPath=blog/content/post/我的家用NAS应用场景/4.png picgo -c ~/.config/picgo/data.json upload /home/zfj/workspaces/blog/content/post/我的家用NAS应用场景/3.png
uploadPath=blog/content/post/我的家用NAS应用场景/5.png picgo -c ~/.config/picgo/data.json upload /home/zfj/workspaces/blog/content/post/我的家用NAS应用场景/4.png
uploadPath=blog/content/post/记录下曾经的电子玩具/9.png picgo -c ~/.config/picgo/data.json upload /home/zfj/workspaces/blog/content/post/记录下曾经的电子玩具/9.png
uploadPath=blog/content/post/记录下曾经的电子玩具/6.png picgo -c ~/.config/picgo/data.json upload /home/zfj/workspaces/blog/content/post/记录下曾经的电子玩具/6.png

执行脚本批量上传

bash ./1.sh 

批量修改文章引用

find . -iname "*.jpg" >> 2.sh
find . -iname "*.jpeg" >> 2.sh
find . -iname "*.png" >> 2.sh
find . -iname "*.webp" >> 2.sh
find . -iname "*.webm" >> 2.sh
find . -iname "*.rar" >> 2.sh
find . -iname "*.zip" >> 2.sh
find . -iname "*.mp4" >> 2.sh
find . -iname "*.mov" >> 2.sh

vim 2.sh
:set ignorecase  #忽略大小写
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.jpg\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.jpeg\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.png\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.webp\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.webm\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.mp4\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.mov\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.rar\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g
:%s/^\/home\/zfj\/workspaces\/blog\/content\/post\/\(.*\)\/\(.*.zip\)/sed -i "s\/\2\/https:\\\/\\\/image.vitshare.cn\\\/blog\\\/content\\\/post\\\/\1\\\/\2\/g" \/home\/zfj\/workspaces\/blog\/content
\/post\/\1\/index.md /g

最后2.sh内容

sed -i "s/3.jpg/https:\/\/image.vitshare.cn\/blog\/content\/post\/我的家用NAS应用场景\/3.jpg/g" /home/zfj/workspaces/blog/content/post/我的家用NAS应用场景/index.md
sed -i "s/4.jpg/https:\/\/image.vitshare.cn\/blog\/content\/post\/我的家用NAS应用场景\/4.jpg/g" /home/zfj/workspaces/blog/content/post/我的家用NAS应用场景/index.md
sed -i "s/9.jpg/https:\/\/image.vitshare.cn\/blog\/content\/post\/记录下曾经的电子玩具\/9.jpg/g" /home/zfj/workspaces/blog/content/post/记录下曾经的电子玩具/index.md
sed -i "s/6.jpg/https:\/\/image.vitshare.cn\/blog\/content\/post\/记录下曾经的电子玩具\/6.jpg/g" /home/zfj/workspaces/blog/content/post/记录下曾经的电子玩具/index.md
sed -i "s/11.jpg/https:\/\/image.vitshare.cn\/blog\/content\/post\/记录下曾经的电子玩具\/11.jpg/g" /home/zfj/workspaces/blog/content/post/记录下曾经的电子玩具/index.md

总结

附件迁移很顺利,到时cloudfalre上跨域名访问折腾了半天。一直返回403(跨域访问),排除了半天最后发现是Hotlink Protection策略惹的祸。

已持续更新 · 发表120篇文章 · 总计93.54k字
 赣ICP备18015110号-1   
使用 Hugo 构建 主题 StackJimmy 设计