1.引用又拍云SDK

requirements.txt文件内新增以下内容,SDK版本可查看又拍云文档中心调整

1
upyun==2.5.5

2.开发又拍云上传接口

hexoweb\libs\image\providers文件夹下新增upyun.py文件

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
from datetime import date
import upyun
from time import time
from hashlib import md5

from ..core import Provider


class Upyun(Provider):
name = '又拍云'
params = {
'bucket': {'description': '储存桶名', 'placeholder': 'Bucket 名称'},
'user_name': {'description': '操作员名', 'placeholder': '操作员名'},
'password': {'description': '操作员密码', 'placeholder': '操作员密码'},
'path': {'description': '保存路径', 'placeholder': '文件上传后保存的路径 包含文件名'},
'prev_url': {'description': '自定义域名', 'placeholder': '最终返回的链接为自定义域名(会自动拼接path)'}
}

def __init__(self, bucket, user_name, password, path, prev_url):
self.bucket = bucket
self.user_name = user_name
self.password = password
self.path = path
self.prev_url = prev_url

def upload(self, file):
now = date.today()
photo_stream = file.read()
file_md5 = md5(photo_stream).hexdigest()
path = self.path.replace("{year}", str(now.year)) \
.replace("{month}", str(now.month)).replace("{day}", str(now.day)) \
.replace("{filename}", file.name[0:-len(file.name.split(".")[-1]) - 1]) \
.replace("{extName}", file.name.split(".")[-1]) \
.replace("{md5}", file_md5)
up = upyun.UpYun(self.bucket, username=self.user_name, password=self.password)
up.put(path, photo_stream, checksum=False, headers={})
return self.prev_url + path

3.增加又拍云图床入口

hexoweb\libs\image\providers\__init__.py文件中,增加以下内容

1
2
3
from . import upyun
# 记得在上一行末尾添加英文逗号
upyun.Upyun.name: upyun.Upyun

https://upyun.gzling.top/hexo/2023/4/image.png

4.在Qexo管理面板填写配置

  1. 下拉选择又拍云
  2. 填写又拍云配置信息,保存即可完成