-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuimg.py
36 lines (27 loc) · 889 Bytes
/
uimg.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
# -*- coding: utf-8 -*-
import qiniu
import os, random, string
from config import *
# 生成5位随机文件名
def random_name():
return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(5))
# 上传至七牛云
def upload_img(fn, sfx='.jpg'):
key = random_name() + sfx
q = qiniu.Auth(QINIU_AK, QINIU_SK)
token = q.upload_token(QINIU_BUCKET, key, 3600)
ret, info = qiniu.put_file(token, key, fn)
if ret != None and ret['key'] == key and ret['hash'] == qiniu.etag(fn):
return QINIU_DOMAIN + key
else:
return False
def run():
upRes = upload_img('/tmp/snap_shutter.jpg')
if upRes:
os.system('notify-send 上传成功 -t 3000')
print('')
else:
os.system('notify-send 上传失败 -t 3000')
print('UPLOAD FAILED')
if __name__ == '__main__':
run()