Skip to content

Commit

Permalink
Merge pull request #50 from dt3310321/s3
Browse files Browse the repository at this point in the history
support py2 and py3
  • Loading branch information
dt3310321 authored May 8, 2018
2 parents 850ddb6 + c5e5ed7 commit fe778db
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 336 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ language: python
python:
- '2.6'
- '2.7'
- '3.5'
- '3.6'
install:
- pip install requests
- pip install six
- pip install nose
- pip install pep8
- pip install dicttoxml
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Qcloud COSv5 SDK
介绍
_______

腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7
腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x

安装指南
__________
Expand Down
46 changes: 18 additions & 28 deletions qcloud_cos/cos_auth.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-

from six.moves.urllib.parse import quote, unquote, urlparse, urlencode
import hmac
import time
import urllib
import hashlib
import logging
from urllib import quote
from urlparse import urlparse
from requests.auth import AuthBase
from .cos_comm import to_unicode, to_bytes
logger = logging.getLogger(__name__)


Expand All @@ -18,62 +17,53 @@ def filter_headers(data):
:return(dict): 计算进签名的头部.
"""
headers = {}
for i in data.keys():
for i in data:
if i == 'Content-Type' or i == 'Host' or i[0] == 'x' or i[0] == 'X':
headers[i] = data[i]
return headers


def to_string(data):
"""转换unicode为string.
:param data(unicode|string): 待转换的unicode|string.
:return(string): 转换后的string.
"""
if isinstance(data, unicode):
return data.encode('utf8')
return data


class CosS3Auth(AuthBase):

def __init__(self, secret_id, secret_key, key='', params={}, expire=10000):
self._secret_id = to_string(secret_id)
self._secret_key = to_string(secret_key)
def __init__(self, secret_id, secret_key, key=None, params={}, expire=10000):
self._secret_id = secret_id
self._secret_key = secret_key
self._expire = expire
self._params = params
if key:
if key[0] == '/':
key = to_unicode(key)
if key[0] == u'/':
self._path = key
else:
self._path = '/' + key
self._path = u'/' + key
else:
self._path = '/'
self._path = u'/'

def __call__(self, r):
path = self._path
uri_params = self._params
headers = filter_headers(r.headers)
uri_params = dict([(k.lower(), v) for k, v in uri_params.items()])
# reserved keywords in headers urlencode are -_.~, notice that / should be encoded and space should not be encoded to plus sign(+)
headers = dict([(k.lower(), quote(v, '-_.~')) for k, v in headers.items()]) # headers中的key转换为小写,value进行encode
format_str = "{method}\n{host}\n{params}\n{headers}\n".format(
headers = dict([(k.lower(), quote(to_bytes(v), '-_.~')) for k, v in headers.items()]) # headers中的key转换为小写,value进行encode
uri_params = dict([(k.lower(), v) for k, v in uri_params.items()])
format_str = u"{method}\n{host}\n{params}\n{headers}\n".format(
method=r.method.lower(),
host=path,
params=urllib.urlencode(sorted(uri_params.items())).replace('+', '%20'),
headers='&'.join(map(lambda (x, y): "%s=%s" % (x, y), sorted(headers.items())))
params=urlencode(sorted(uri_params.items())).replace('+', '%20'),
headers='&'.join(map(lambda tupl: "%s=%s" % (tupl[0], tupl[1]), sorted(headers.items())))
)
logger.debug("format str: " + format_str)

start_sign_time = int(time.time())
sign_time = "{bg_time};{ed_time}".format(bg_time=start_sign_time-60, ed_time=start_sign_time+self._expire)
sha1 = hashlib.sha1()
sha1.update(format_str)
sha1.update(to_bytes(format_str))

str_to_sign = "sha1\n{time}\n{sha1}\n".format(time=sign_time, sha1=sha1.hexdigest())
logger.debug('str_to_sign: ' + str(str_to_sign))
sign_key = hmac.new(self._secret_key, sign_time, hashlib.sha1).hexdigest()
sign = hmac.new(sign_key, str_to_sign, hashlib.sha1).hexdigest()
sign_key = hmac.new(to_bytes(self._secret_key), to_bytes(sign_time), hashlib.sha1).hexdigest()
sign = hmac.new(to_bytes(sign_key), to_bytes(str_to_sign), hashlib.sha1).hexdigest()
logger.debug('sign_key: ' + str(sign_key))
logger.debug('sign: ' + str(sign))
sign_tpl = "q-sign-algorithm=sha1&q-ak={ak}&q-sign-time={sign_time}&q-key-time={key_time}&q-header-list={headers}&q-url-param-list={params}&q-signature={sign}"
Expand Down
Loading

0 comments on commit fe778db

Please sign in to comment.