Skip to content

Commit

Permalink
optimize some code
Browse files Browse the repository at this point in the history
  • Loading branch information
wujinhu committed Nov 21, 2017
1 parent a6d6b51 commit 5c545ac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ install:
- pip install --upgrade mock
script:
- nosetests unittests/ --with-cov
- export OSS_AUTH_VERSION=v1
- export OSS_TEST_AUTH_VERSION=v1
- if [ -n "$OSS_TEST_ACCESS_KEY_ID" ]; then travis_wait 30 nosetests -s tests/; fi
- export OSS_AUTH_VERSION=v2
- export OSS_TEST_AUTH_VERSION=v2
- if [ -n "$OSS_TEST_ACCESS_KEY_ID" ]; then travis_wait 30 nosetests -s tests/ --with-cov; fi
after_success:
- coveralls
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Run the test in the following method:
You can set environment variable to test sign v2:

.. code-blocks:: bash
.. code-block:: bash
$ export OSS_TEST_AUTH_VERSION=v2
Expand Down
2 changes: 1 addition & 1 deletion examples/sign_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
assert '<' not in param, 'Please set variable: ' + param


# 用oss2.SIGN_VERSION_2创建一个Auth对象,这样我们就可以用V2算法来签名请求
# 用oss2.SIGN_VERSION_2创建一个Auth对象,这样我们就可以用V2算法来签名请求。默认采用V1算法
auth = oss2.Auth(access_key_id, access_key_secret, sign_version=oss2.SIGN_VERSION_2)

# 创建一个Bucket,利用它进行所有bucket与object相关操作
Expand Down
29 changes: 18 additions & 11 deletions oss2/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@
SIGN_VERSION_2 = 'v2'


def make_sign(access_key_id, access_key_secret, sign_version):
if sign_version == SIGN_VERSION_2:
return SignV2(access_key_id.strip(), access_key_secret.strip())
else:
return SignV1(access_key_id.strip(), access_key_secret.strip())


class Auth(object):
"""用户访问凭证,需要传入AccessKeyID与AccessKeySecret"""
def __init__(self, access_key_id, access_key_secret, sign_version=SIGN_VERSION_2):
if sign_version == SIGN_VERSION_2:
self.__sign = SignV2(access_key_id.strip(), access_key_secret.strip())
else:
self.__sign = SignV1(access_key_id.strip(), access_key_secret.strip())
def __init__(self, access_key_id, access_key_secret, sign_version=SIGN_VERSION_1):
"""
初始化self.__sign
:param access_key_id:
:param access_key_secret:
:param sign_version: 需要生成sign的版本,默认为SIGN_VERSION_1(v1)
"""
self.__sign = make_sign(access_key_id, access_key_secret, sign_version)

def _sign_request(self, req, bucket_name, key):
self.__sign._sign_request(req, bucket_name, key)
Expand Down Expand Up @@ -191,13 +201,10 @@ class StsAuth(object):
:param str access_key_id: 临时AccessKeyId
:param str access_key_secret: 临时AccessKeySecret
:param str security_token: 临时安全令牌(SecurityToken)
:param str sign_version: 需要生成sign的版本,默认为SIGN_VERSION_1(v1)
"""
def __init__(self, access_key_id, access_key_secret, security_token, sign_version=SIGN_VERSION_2):
if sign_version == SIGN_VERSION_2:
self.__sign = SignV2(access_key_id.strip(), access_key_secret.strip())
else:
self.__sign = SignV1(access_key_id.strip(), access_key_secret.strip())

def __init__(self, access_key_id, access_key_secret, security_token, sign_version=SIGN_VERSION_1):
self.__sign = make_sign(access_key_id, access_key_secret, sign_version)
self.__security_token = security_token

def _sign_request(self, req, bucket_name, key):
Expand Down

0 comments on commit 5c545ac

Please sign in to comment.