Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK V2 #16

Merged
merged 41 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
8bf3aca
Modify Copy Object
dt3310321 Sep 27, 2017
eb0e7ce
Add Cors And Lifecycle
dt3310321 Sep 28, 2017
4dbae21
Add dicttoxml
dt3310321 Sep 28, 2017
585f094
change travis
dt3310321 Sep 28, 2017
09274a2
remove 2q
dt3310321 Sep 28, 2017
e754b25
Modify test.py
dt3310321 Sep 28, 2017
cc372f9
Modify ACL
dt3310321 Sep 29, 2017
7722034
Merge branch 'master' of github.com:tencentyun/cos-python-sdk-v5 into s3
dt3310321 Sep 29, 2017
a57e6a9
Modify the Auth Module
dt3310321 Sep 29, 2017
cfe079f
Modify Auth Moudle
dt3310321 Sep 29, 2017
936f0fb
Add params to auth
dt3310321 Sep 29, 2017
2b76404
Modify Reverse Character
dt3310321 Sep 29, 2017
a81d0f8
reserved keywords in headers urlencode are -_.~, notice that / should…
dt3310321 Sep 30, 2017
223b512
Filter headers to auth
dt3310321 Sep 30, 2017
91aa5a4
Only Auth the ?acl format params
dt3310321 Sep 30, 2017
5439078
only auth the ?acl format params
dt3310321 Sep 30, 2017
1a0832f
Update README.rst
dt3310321 Sep 30, 2017
6e7f2f9
Modify Copy Object
dt3310321 Oct 9, 2017
8f3721c
Modify demo
dt3310321 Oct 9, 2017
0cd71c7
Modify list objects
dt3310321 Oct 9, 2017
99a8bbf
Modify Acl type to Type
dt3310321 Oct 10, 2017
f83e3cc
Support all special characters
dt3310321 Oct 11, 2017
c049a2f
use cos style to encode path
dt3310321 Oct 11, 2017
effc466
copy object support metadata
dt3310321 Oct 13, 2017
29e74d1
Modify default params
dt3310321 Oct 17, 2017
9956318
Modify put/get object return
dt3310321 Oct 18, 2017
1ca764a
Add if assert for copy object
dt3310321 Oct 18, 2017
6fd2e7e
Add Bucket versioning,location,head
dt3310321 Oct 19, 2017
4953c3c
Add bucket versioning,location,head
dt3310321 Oct 19, 2017
4767d64
Modify Readme Config
dt3310321 Oct 23, 2017
7055f04
Modify Readme
dt3310321 Oct 23, 2017
755878f
Add standard annotation
dt3310321 Oct 23, 2017
abb4eaa
Modify Copy Demo
dt3310321 Oct 23, 2017
fe56674
Add demo for upload/download
dt3310321 Oct 23, 2017
1677281
Modify demo.py
dt3310321 Oct 23, 2017
a66dfe0
fix pep8
dt3310321 Oct 23, 2017
35254fa
Modify requirements
dt3310321 Oct 26, 2017
0a5e082
Modify versioning status
dt3310321 Oct 26, 2017
5d04504
add license
dt3310321 Oct 26, 2017
3a6766e
Fix up hmac param can not be unicode
dt3310321 Oct 26, 2017
a031974
Add Travis PyPI deployment
dt3310321 Oct 30, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
sudo: false
language: python

python:
- "2.6"
- "2.7"

install:
- pip install requests
- pip install nose
- pip install pep8

script:
- pep8 --max-line-length=180 qcloud_cos/.
- nosetests -s -v
sudo: false
language: python
python:
- '2.6'
- '2.7'
install:
- pip install requests
- pip install nose
- pip install pep8
- pip install dicttoxml
script:
- pep8 --max-line-length=180 qcloud_cos/.
- nosetests -s -v
deploy:
provider: pypi
distributions: "sdist bdist bdist_wheel"
user: dt3310321
password:
secure: LPiyxt41FBOXUM4WyzMjQvFz9ctQMcJQl7fTKkZ58fgD15XIWb/JXG2+tpCF/hFe6Fa8Kk1qL3cD+FxnQMWeTftebeWz35cn3EqWpd43/5BoSXjf0n2XTg2Ere6USCmTlosmW6A3GF18+DtsD2xzWTf6uWzdpJCHpBdNaxuph4poLfd4A/SCmggfEi7MN8d99sdJ8ENa3iUo51TWqCt5o57N7QLMII2nW8UpntP2XJ5aHZD1KDd8J8RMuDjtGBOvKbWjKCdFSZXT2Rcj/BDsacCUyACnDXdUAQ+Ic55eTtgpQFc7vm9yk5ApNrLC0xtf3WspO0HZO0ZgzsT1a5eGYYckeh2uVudCeC3VMgp3NQE7bLsO/ZLAhg8mQKr16bUNF+rfs59CL1/8/CeLxuIgKm6MnT1NRMsltdDqPjPsQTeEXpZQZ7urgMrXPCte8HgcCsgfZe39v0TrzZ4jES3orB68MnIsusDzoDfQzN2Z4UkkEEktihEwRjel6vzzMs1XGhC0ksvV+Tb6YUpdpnx6FO9ioZJS4ml4kDk3y8iowRcXBOvtMqrA7QwGmwpBARqts6bSa6ziXNqzFiiGWwbY7eeFscsX3uy84+5UIkABRylSl5C+C2B6jEXFD2T5r7YBQ+30eoPucLELyPxVgMkWTZTg8BVOfM2do4fkZQ+H0Rk=
on:
tags: true
branch: master
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 腾讯云

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224
.. code:: python

# 设置用户属性, 包括appid, secret_id, secret_key, region
appid = 100000 # 替换为用户的appid
secret_id = u'xxxxxxxx' # 替换为用户的secret_id
secret_key = u'xxxxxxx' # 替换为用户的secret_key
  region = "ap-beiging-1"    # 替换为用户的region
token = '' # 使用临时秘钥需要传入Token,默认为空,可不填
appid = '100000' # 替换为用户的appid
secret_id = 'xxxxxxxx' # 替换为用户的secret_id
secret_key = 'xxxxxxx' # 替换为用户的secret_key
  region = 'ap-beiging-1'   # 替换为用户的region
token = '' # 使用临时秘钥需要传入Token,默认为空,可不填
config = CosConfig(Appid=appid, Region=region, Access_id=secret_id, Access_key=secret_key, Token=token) #获取配置对象
client = CosS3Client(config) #获取客户端对象

Expand Down
71 changes: 46 additions & 25 deletions qcloud_cos/cos_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,61 @@
logger = logging.getLogger(__name__)


def filter_headers(data):
"""只设置host content-type 还有x开头的头部.

:param data(dict): 所有的头部信息.
:return(dict): 计算进签名的头部.
"""
headers = {}
for i in data.keys():
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, access_id, secret_key, expire=10000):
self._access_id = access_id
self._secret_key = secret_key
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)
self._expire = expire
self._params = params
if key:
if key[0] == '/':
self._path = key
else:
self._path = '/' + key
else:
self._path = '/'

def __call__(self, r):
method = r.method.lower()
uri = urllib.unquote(r.url)
uri = uri.split('?')[0]
http_header = r.headers
r.headers = {}
rt = urlparse(uri)
logger.debug("url parse: " + str(rt))
if rt.query != "" and ("&" in rt.query or '=' in rt.query):
uri_params = dict(map(lambda s: s.lower().split('='), rt.query.split('&')))
elif rt.query != "":
uri_params = {rt.query: ""}
else:
uri_params = {}
headers = dict([(k.lower(), quote(v).lower()) for k, v in r.headers.items()])
path = self._path
uri_params = self._params
headers = filter_headers(r.headers)
# 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(
method=method.lower(),
host=rt.path,
params=urllib.urlencode(uri_params),
method=r.method.lower(),
host=path,
params=urllib.urlencode(sorted(uri_params.items())),
headers='&'.join(map(lambda (x, y): "%s=%s" % (x, y), 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)
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)

Expand All @@ -54,18 +77,16 @@ def __call__(self, r):
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}"

http_header['Authorization'] = sign_tpl.format(
ak=self._access_id,
r.headers['Authorization'] = sign_tpl.format(
ak=self._secret_id,
sign_time=sign_time,
key_time=sign_time,
params=';'.join(sorted(map(lambda k: k.lower(), uri_params.keys()))),
headers=';'.join(sorted(headers.keys())),
sign=sign
)
r.headers = http_header
logger.debug("sign_key" + str(sign_key))
logger.debug(r.headers['Authorization'])

logger.debug("request headers: " + str(r.headers))
return r

Expand Down
Loading