用过一段时间的PicGo,但是感觉太重了,为了一个上传图片去装一个软件有点太麻烦。 然后目前个人感觉也就Gitlab速度还可以一点,于是昨天翻了官方文档踩了很多坑,总算搞好了。 现将关键代码放到这里。
file_path = datetime.datetime.now().strftime('zb_users/upload/%Y/%m/')
headers = {'PRIVATE-TOKEN': '这里放你的项目token'}
img = file.file.read()
ext = file.content_type.split('/')[-1]
file_path = urllib.parse.quote(file_path + uuid.uuid4().hex + '.' + ext).replace('/', '%2f')
url = f'https://gitlab.com/api/v4/projects/这里放你的项目ID/repository/files/{file_path}'
response = await requests.post(url, headers=headers, json={
'branch': 'master',
'content': base64.b64encode(img).decode('utf-8'),
'author_email': [email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p,m,o){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-yjshash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-yjsemail')){for(e='',o=0,r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2){m=('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);if((a.length-n)<=6&&a.length>=128)o=(parseInt(m)<=191)?1:o*2;if(o>1)break;e+='%'+m;}p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */',
'author_name': 'Lan',
'encoding': 'base64',
'commit_message': 'www.lanol.cn'
})
res = await response.json()
然后花了点时间写了个小前端用来上传,支持选择,拖拽,粘贴文件进行上传。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有