-
Notifications
You must be signed in to change notification settings - Fork 1
/
qiniu_api.py
55 lines (50 loc) · 1.72 KB
/
qiniu_api.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
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
import os
import uuid
from qiniu import Auth, put_data, BucketManager
from key import ACCESS_KEY, SECRET_KEY, BUCKET_NAME
q = Auth(ACCESS_KEY, SECRET_KEY)
class Qiniu(object):
# def save_file_to_qiniu(self,
# upload_file,
# filename=str(uuid.uuid1()).replace('-', ''),
# path='attach'):
# try:
# key = '%s/%s' % (path, filename)
# token = self.q.upload_token(BUCKET_NAME, key)
# ret, info = put_data(token, key, upload_file)
# if ret.get('key', None) == None:
# raise Exception('upload error')
# else:
# return u'%s' % key
# except Exception, e:
# print str(e)
# return str(e)
def fetch_file_to_qiniu(self,
url,
filename=str(uuid.uuid1()).replace('-', ''),
path='attach'):
try:
key = '%s/%s' % (path, filename)
bucket = BucketManager(q)
ret, info = bucket.fetch(url, BUCKET_NAME, key)
if ret.get('key', None) == None:
raise Exception('fetch error')
else:
return u'%s' % key
except Exception, e:
print str(e)
return str(e)
def list_file_from_qiniu(self):
info = None
prefix = 'images'
limit = None
delimiter = None
marker = None
try:
bucket = BucketManager(q)
ret, eof, info = bucket.list(BUCKET_NAME, prefix, marker, limit, delimiter)
except Exception, e:
pass
finally:
return info