Example #1
0
    def app_sign(self, bucket, fileid, expired):
        app_info = conf.get_app_info()
        if not self._secret_id or not self._secret_key or not app_info['appid']:
            return self.AUTH_SECRET_ID_KEY_ERROR

        now = int(time.time())
        rdm = random.randint(0, 999999999)
        plain_text = 'a=' + app_info['appid'] + '&k=' + self._secret_id + '&e=' + str(expired) + '&t=' + str(now) + '&r=' + str(rdm) + '&f=' + fileid + '&b=' + bucket 
        bin = hmac.new(self._secret_key, plain_text, hashlib.sha1)
        s = bin.hexdigest()
        s = binascii.unhexlify(s)
        s = s + plain_text
        signature = base64.b64encode(s).rstrip()    #生成签名
        return signature
Example #2
0
	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))
Example #3
0
    def app_sign(self, bucket, fileid, expired):
        app_info = conf.get_app_info()
        if not self._secret_id or not self._secret_key or not app_info['appid']:
            return self.AUTH_SECRET_ID_KEY_ERROR

        now = int(time.time())
        rdm = random.randint(0, 999999999)
        plain_text = 'a=' + app_info[
            'appid'] + '&k=' + self._secret_id + '&e=' + str(
                expired) + '&t=' + str(now) + '&r=' + str(
                    rdm) + '&f=' + fileid + '&b=' + bucket
        bin = hmac.new(self._secret_key, plain_text, hashlib.sha1)
        s = bin.hexdigest()
        s = binascii.unhexlify(s)
        s = s + plain_text
        signature = base64.b64encode(s).rstrip()  #生成签名
        return signature
Example #4
0
	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))
Example #5
0
 def get_info_from_url(self, url):
     app_info = conf.get_app_info()
     end_point = app_info['end_point']
     info = urlparse(url)
     end_point_info = urlparse(end_point)
     if (info.hostname == urlparse(conf.API_COS_END_POINT).hostname) :
         if info.path :
             parts = info.path.split('/')
             if len(parts) >= 5:
                 cate = parts[1]
                 ver = parts[2]
                 appid = parts[3]
                 bucket = parts[4]
                 fileid = ''
                 for i in range(3, len(parts)) :
                     fileid += '/' + parts[i]
                 return {'cate':cate, 'ver':ver, 'appid':appid, 'bucket':bucket, 'fileid':fileid}
             else:
                 return {}
         else:
             return {}
     else :
         return {}
Example #6
0
	def generate_res_url(self, bucket, dstpath):
		app_info = conf.get_app_info()
		return app_info['end_point'] + str(app_info['appid']) + '/' + bucket + '/' + dstpath