def __stat(self, bucket, path): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(bucket, path) auth = Auth(self._secret_id, self._secret_key) sign = auth.sign_more(bucket, expired) headers = { 'Authorization':sign, 'User-Agent':conf.get_ua(), } data={'op':'stat'} return self.sendRequest('GET', url, headers=headers, params=data, timeout=(self.connect_timeout, self.read_timeout))
def __list(self, bucket, path, num=20, pattern='eListBoth', order=0, context='') : expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(bucket, path) auth = Auth(self._secret_id, self._secret_key) sign = auth.sign_more(bucket, expired) headers = { 'Authorization':sign, 'User-Agent':conf.get_ua(), } data = {'op':'list','num':num,'pattern':pattern,'order':order,'context':context} return self.sendRequest('GET', url, headers=headers, params=data, timeout=(self.connect_timeout, self.read_timeout))
def __update(self, bucket, path, bizattr=''): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(bucket, path) auth = Auth(self._secret_id, self._secret_key) sign = auth.sign_once(bucket, '/'+str(conf.get_app_info()['appid'])+'/'+bucket+'/'+path) #sign = auth.sign_once(bucket, '/'+path) headers = { 'Authorization':sign, 'Content-Type':'application/json', 'User-Agent':conf.get_ua(), } data = {'op':'update','biz_attr':bizattr} return self.sendRequest('POST', url, headers=headers, data=json.dumps(data), timeout=(self.connect_timeout, self.read_timeout))
def upload_data(self,bucket,dstpath,data,session,offset): url = self.generate_res_url(bucket,dstpath) expired = int(time.time()) + self.EXPIRED_SECONDS auth = Auth(self._secret_id, self._secret_key) sha1 = hashlib.sha1(); sha1.update(data) sign = auth.sign_more(bucket, expired) headers = { 'Authorization':sign, 'User-Agent':conf.get_ua(), } files = {'op': ('upload_slice'),'filecontent': data,'sha':sha1.hexdigest(),'session':session,'offset':str(offset)} return self.sendRequest('POST', url, headers=headers, files=files, timeout=(self.connect_timeout, self.read_timeout))
def createFolder(self, bucket, path, bizattr=''): expired = int(time.time()) + self.EXPIRED_SECONDS bucket = string.strip(bucket, '/') path = urllib.quote(string.strip(path, '/') + '/', '~/') url = self.generate_res_url(bucket, path) auth = Auth(self._secret_id, self._secret_key) sign = auth.sign_more(bucket, expired) headers = { 'Authorization':sign, 'Content-Type':'application/json', 'User-Agent':conf.get_ua(), } data = {'op':'create','biz_attr':bizattr} return self.sendRequest('POST', url, headers=headers, data=json.dumps(data), timeout=(self.connect_timeout, self.read_timeout))
def __delete(self, bucket, path): if path == '' or path == '/': return {'httpcode':0, 'code':self.COS_PARAMS_ERROR, 'message':'path cannot be empty', 'data':{}} expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(bucket, path) auth = Auth(self._secret_id, self._secret_key) sign = auth.sign_once(bucket, '/'+str(conf.get_app_info()['appid'])+'/'+bucket+'/'+path) #sign = auth.sign_once(bucket, '/'+path) headers = { 'Authorization':sign, 'Content-Type':'application/json', 'User-Agent':conf.get_ua(), } data = {'op':'delete'} return self.sendRequest('POST', url, headers=headers, data=json.dumps(data), timeout=(self.connect_timeout, self.read_timeout))
def upload_buffer(self, buffer, bucket, dstpath, bizattr=''): expired = int(time.time()) + self.EXPIRED_SECONDS bucket = string.strip(bucket, '/') dstpath = urllib.quote(string.strip(dstpath, '/'), '~/') url = self.generate_res_url(bucket, dstpath) auth = Auth(self._secret_id, self._secret_key) sha1 = hashlib.sha1(); sha1.update(buffer) sign = auth.sign_more(bucket, expired) headers = { 'Authorization':sign, 'User-Agent':conf.get_ua(), } files = {'op':'upload','filecontent':buffer,'sha':sha1.hexdigest(),'biz_attr':bizattr} return self.sendRequest('POST', url, headers=headers, files=files, timeout=(self.connect_timeout, self.read_timeout))
def upload(self, srcpath, bucket, dstpath, bizattr=''): srcpath = os.path.abspath(srcpath); if not os.path.exists(srcpath): return {'httpcode':0, 'code':self.COS_FILE_NOT_EXISTS, 'message':'file not exists', 'data':{}} expired = int(time.time()) + self.EXPIRED_SECONDS bucket = string.strip(bucket, '/') dstpath = urllib.quote(string.strip(dstpath, '/'), '~/') url = self.generate_res_url(bucket, dstpath) auth = Auth(self._secret_id, self._secret_key) sha1 = self.sha1file(srcpath); sign = auth.sign_more(bucket, expired) headers = { 'Authorization':sign, 'User-Agent':conf.get_ua(), } files = {'op':'upload','filecontent':open(srcpath, 'rb'),'sha':sha1,'biz_attr':bizattr} return self.sendRequest('POST', url, headers=headers, files=files, timeout=(self.connect_timeout, self.read_timeout))
def upload_prepare(self,filepath,bucket,dstpath,bizattr,slice_size,session): if not os.path.exists(filepath): return {'httpcode':0, 'code':self.COS_FILE_NOT_EXISTS, 'message':'file not exists', 'data':{}} url = self.generate_res_url(bucket, dstpath) expired = int(time.time()) + self.EXPIRED_SECONDS auth = Auth(self._secret_id, self._secret_key) size = os.path.getsize(filepath) sha1 = self.sha1file(filepath); sign = auth.sign_more(bucket, expired) headers = { 'Authorization':sign, 'User-Agent':conf.get_ua(), } files = {'op': ('upload_slice'),'sha':sha1,'filesize': str(size)} if bizattr != '': files['biz_attr'] = bizattr if slice_size > 0: files['slice_size'] = str(slice_size) if session != '': files['session'] = session return self.sendRequest('POST', url, headers=headers,files=files, timeout=(self.connect_timeout, self.read_timeout))