-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.py
44 lines (38 loc) · 1.1 KB
/
storage.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
38
39
40
41
42
43
44
# 储存服务相关
import requests
import time
import config
import hashlib
import backblaze
def randomString(length=16):
import random
import string
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
def xuehai(file, md5):
url = config.XUEHAI_URL
payload = {}
files=[
('files',(randomString() + '.jpg', file, 'image/jpeg')),
]
headers = {
'XueHai-MD5': md5,
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
return (response.json())["uploadFileDTO"]["fileId"]
def upload(path):
if config.STORAGE == "xuehai":
with open(path, 'rb') as f:
md5 = hashlib.md5(f.read()).hexdigest()
# 上传至学海OSS
with open(path, 'rb') as f:
fileId = xuehai(f, md5)
return fileId
elif config.STORAGE == "backblaze":
backblaze.bbUpload(path)
else:
return None
def getBBImage(fileId):
return backblaze.bbDownload(fileId)
if __name__ == '__main__':
fileId = upload("./exampleImg/ikun.jpg")
print(fileId)