前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图床,通过Python调用api上传图片到Gitlab

图床,通过Python调用api上传图片到Gitlab

作者头像
SingYi
发布2023-08-23 08:09:01
3340
发布2023-08-23 08:09:01
举报
文章被收录于专栏:Lan小站

用过一段时间的PicGo,但是感觉太重了,为了一个上传图片去装一个软件有点太麻烦。 然后目前个人感觉也就Gitlab速度还可以一点,于是昨天翻了官方文档踩了很多坑,总算搞好了。 现将关键代码放到这里。

代码语言:javascript
复制
    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()

然后花了点时间写了个小前端用来上传,支持选择,拖拽,粘贴文件进行上传。

www.lanol.cn
www.lanol.cn
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年11月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档