昨天将Hugo博客迁移到Hexo,网站也重新部署到了码云,便想着干脆把图床也部署到码云,这样图片加载的速度也会变快,用PicGo上传的时候也不会总是报错了。😜

参考大佬文章——PICGO+Gitee搭建图床

1. 安装GitHub-Plus插件

打开PicGo(没有安装的小伙伴移步我这篇博客使用PicGo+GitHub搭建个人图床),选择插件设置,搜索github,下载github-plus插件。

安装插件报错的话,先安装Node.js🤗


2. 图床设置

在Gitee上新建仓库,权限设置为公开。(已经在github建过图床的点击最下面的导入github仓库,复制仓库链接就可以把之间的文件都同步过来啦。)

在PicGo中点击图床设置-githubPlus

  • repo 填写 用户名/仓库名
  • branch 仓库分支名
  • path 图片保存路径
  • origin 修改为gitee
  • token 令牌,与github类似,下面会介绍如何获取。

3. 获取token

打开码云,登录自己账号之后,点开设置,找到私人令牌,创建私人令牌。

权限选择projects即可~

点击提交,就已经成功啦,速度飞快。


4. 批量修改之前博客的图床链接

之前图床在github上的时候,已经写了挺多篇博客的了。图床迁移之后,需要修改这些博客中引用的图片链接。

1
2
3
4
5
//Github上的图床链接格式
![](https://raw.githubusercontent.com/shenshilei1022/hugo_image_resource/master/imgs/TrakaiLithuania_ZH-CN0447602818_1920x1080.jpg)

//Gitee上的图床链接格式
![](https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/TrakaiLithuania_ZH-CN0447602818_1920x1080.jpg)

可以看到两个链接只有master前面的地方不一样,那么只需要找到文件中的图床链接,把链接以master分割,再加上"https://gitee.com/shenshilei1022/hugo_image_resource/raw/master"即可构成新的图床链接。

我参考了这位大佬的Python脚本——博客图床迁移记,自己做了一些修改。

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
85
86
87
88
89
90
91
import os
import re

class ReplaceImage:

path = ''
lineNum = 0
s = r'http[s]?://(?:raw.githubusercontent.com)/.*?(?:jpg|png|JPG)'
subpath = ''
failednum = 0

def __init__(self):
self.url_re = re.compile(self.s)

def search(self, path):
self.path = path
self.handleDir(path)

def handleDir(self, path):
dirs = os.listdir(path)
for d in dirs:
subpath = os.path.join(path, d)
if os.path.isfile(subpath) and subpath.endswith(".md"):
self.handleFile(subpath)
elif os.path.isdir(subpath):
self.handleDir(subpath)

print("program end")

def handleFile(self, fileName):
print("\n")
print("start read file %s..." % fileName)
self.subpath = fileName

# 注意编码
f = open(fileName, 'r+',encoding="utf-8",errors="ignore")
self.lineNum = 1
data = ""
while True:
line = f.readline()
if not line:
break
line = self.replaceImage(line)
self.lineNum = self.lineNum + 1
data += line
f.close()

# 注意编码
with open(fileName, "w+",encoding="utf-8",errors="ignore") as f:
f.writelines(data)

def replaceImage(self, line):

searchResult = self.searchImage(line)

if not searchResult:
return line
oldline = line

for result in searchResult:
# 这里的replace_url就是将要替换的Gitee图床链接
replace_url = self.uploadImage(result)
line = self.replaceLine(line, result, replace_url)

print("before replace is %s" % oldline)
print("after replace is %s" % line)

return line

def searchImage(self, line):
if self.url_re.search(line):
all_search = search.url_re.findall(line)
return all_search
else:
return []

def replaceLine(self, line, search, url):
return line.replace(search, url)

def uploadImage(self, url):
subUrl = url.split("master")[1]
targetUrl = "https://gitee.com/shenshilei1022/hugo_image_resource/raw/master" + subUrl
return targetUrl



if __name__ == "__main__":
search = ReplaceImage()
print("please input dir path and api key:\n")
dir = input("dir:")
search.search(dir)

运行之后输入post路径就可把目录下的文章的图片都换成Gitee图床链接啦😃


5. 参考