原作者开源了,我就把我这里的也发出来了。
侵删
import requests,json,os,hashlib,sys,pyperclip
from tkinter import *
from tkinter import messagebox as m
class XesUploader:
# 文件相对路径
def upload(self, relativeFilePath):
absolutePath = relativeFilePath
return self.uploadAbsolutePath(absolutePath)
# 文件绝对路径
def uploadAbsolutePath(self, filepath):
md5 = None
contents = None
if os.path.isfile(filepath):
fp = open(filepath, 'rb')
contents = fp.read()
fp.close()
md5 = hashlib.md5(contents).hexdigest()
if md5 is None or contents is None:
raise Exception(f"文件 |{filepath}|不存在")
uploadParams = self._getUploadParams(filepath, md5)
requests.request(method="PUT", url=uploadParams['host'], data=contents, headers=uploadParams['headers'], proxies={"http": None, "https": None})
return uploadParams['url']
# 获取上传签名
def _getUploadParams(self, filename, md5):
url = 'https://code.xueersi.com/api/assets/get_oss_upload_params'
params = {"scene": "offline_python_assets", "md5": md5, "filename": filename}
response = requests.get(url=url, params=params, proxies={"http": None, "https": None})
data = json.loads(response.text)['data']
return data
uploader = XesUploader()
url = uploader.upload(sys.argv[1])
pyperclip.copy(url)
tk = Tk()
tk.withdraw()
m.showinfo("成功","已复制"+url)