/
bucket.py
80 lines (66 loc) · 2.52 KB
/
bucket.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#coding=utf-8
from qiniu import conf as qConf, rs as qRs, io as qIo, resumable_io as qRIo, rsf as qRsf
import logging
import os
import urllib
def utilItem2Str(item):
rsKey = item['key'].encode('utf8')
rsHash = str(item['hash'])
#fsize = item['fsize']
#mimeType = str(item['mimeType'])
#putTime = item['putTime']
#return '%s %s %d %s %d' % (fkey, fhash, fsize, mimeType, putTime)
return '%s %s' % (rsKey, rsHash)
class QBucketError(Exception):
def __init__(self, message=None):
Exception.__init__(self, "%s" % (message,))
class QBucket(object):
def __init__(self, bucket, domain, accessKey, secretKey):
if not (bucket and domain and accessKey and secretKey):
raise QBucketError('')
self.bucket = bucket
self.domain = domain
self.accessKey = accessKey
self.secretKey = secretKey
qConf.ACCESS_KEY = accessKey
qConf.SECRET_KEY = secretKey
self._urlopen = urllib.URLopener()
self.initialize()
return
def initialize(self):
return
def listAll(self):
client = qRsf.Client()
itemList = []
marker = 'begin'
while marker:
current_m = marker if marker != 'begin' else ''
ret, err = client.list_prefix(self.bucket, prefix='', limit=1000, marker=current_m)
if err and err != 'EOF':
raise QBucketError('Error to get list use marker: %s' % (current_m,))
itemList += ret['items']
marker = ret.get('marker')
items = dict()
for i in itemList:
items[i['key'].encode('utf8')] = str(i['hash'])
return items
def getFile(self, key, localPath):
url = 'http://%s/%s' % (self.domain, urllib.quote(key))
logging.debug('downloading: %s' % (url,))
try:
self._urlopen.retrieve(url, localPath)
logging.debug('downloaded: %s' % (url,))
except Exception as e:
logging.warning('failed to download: Domain:%s ; Key:%s ; Exception:%s'
% (self.domain, key, e))
return
def putFile(self, key, localFile, mimeType=None):
putPolicy = qRs.PutPolicy('%s:%s' % (self.bucket, key))
uptoken = putPolicy.token()
putExtra = qIo.PutExtra()
if mimeType is not None:
putExtra.mime_type = '%s' % (mimeType,)
statInfo = os.stat(localFile)
fsize = statInfo.st_size
ret, err = qIo.put_file(uptoken, key, localFile, putExtra)
return ret, err