Skip to content

S3 #77

New issue

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

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

Already on GitHub? # to your account

Merged
merged 7 commits into from
Nov 20, 2018
Merged

S3 #77

Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ install:
- pip install requests
- pip install six
- pip install nose
- pip install pep8
- pip install pycodestyle
- pip install dicttoxml
script:
- pep8 --max-line-length=180 qcloud_cos/.
- pycodestyle --max-line-length=180 qcloud_cos/.
- nosetests -s -v ut/
deploy:
provider: pypi
Expand Down
11 changes: 11 additions & 0 deletions qcloud_cos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@
from .cos_exception import CosClientError
from .cos_auth import CosS3Auth
from .cos_comm import get_date

import logging

try:
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

logging.getLogger(__name__).addHandler(NullHandler())
19 changes: 10 additions & 9 deletions qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .cos_threadpool import SimpleThreadPool
from .cos_exception import CosClientError
from .cos_exception import CosServiceError
from .version import __version__

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -192,7 +193,7 @@ def send_request(self, method, url, bucket, timeout=30, **kwargs):
"""封装request库发起http请求"""
if self._conf._timeout is not None: # 用户自定义超时时间
timeout = self._conf._timeout
kwargs['headers']['User-Agent'] = 'cos-python-sdk-v5.1.6.1'
kwargs['headers']['User-Agent'] = 'cos-python-sdk-v' + __version__
if self._conf._token is not None:
kwargs['headers']['x-cos-security-token'] = self._conf._token
if bucket is not None:
Expand Down Expand Up @@ -281,7 +282,7 @@ def put_object(self, Bucket, Body, Key, EnableMD5=False, **kwargs):
data=Body,
headers=headers)

response = rt.headers
response = dict(**rt.headers)
return response

def get_object(self, Bucket, Key, **kwargs):
Expand Down Expand Up @@ -332,7 +333,7 @@ def get_object(self, Bucket, Key, **kwargs):
params=params,
headers=headers)

response = rt.headers
response = dict(**rt.headers)
response['Body'] = StreamBody(rt)

return response
Expand Down Expand Up @@ -422,7 +423,7 @@ def delete_object(self, Bucket, Key, **kwargs):
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key),
headers=headers,
params=params)
data = rt.headers
data = dict(**rt.headers)
return data

def delete_objects(self, Bucket, Delete={}, **kwargs):
Expand Down Expand Up @@ -511,7 +512,7 @@ def head_object(self, Bucket, Key, **kwargs):
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key, params=params),
headers=headers,
params=params)
return rt.headers
return dict(**rt.headers)

def copy_object(self, Bucket, Key, CopySource, CopyStatus='Copy', **kwargs):
"""文件拷贝,文件信息修改
Expand Down Expand Up @@ -554,7 +555,7 @@ def copy_object(self, Bucket, Key, CopySource, CopyStatus='Copy', **kwargs):
if 'ETag' not in body:
logger.error(rt.content)
raise CosServiceError('PUT', rt.content, 200)
data = rt.headers
data = dict(**rt.headers)
data.update(body)
return data

Expand Down Expand Up @@ -601,7 +602,7 @@ def upload_part_copy(self, Bucket, Key, PartNumber, UploadId, CopySource, CopySo
params=params,
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key, params=params))
body = xml_to_dict(rt.content)
data = rt.headers
data = dict(**rt.headers)
data.update(body)
return data

Expand Down Expand Up @@ -734,7 +735,7 @@ def complete_multipart_upload(self, Bucket, Key, UploadId, MultipartUpload={}, *
if 'ETag' not in body:
logger.error(rt.content)
raise CosServiceError('POST', rt.content, 200)
data = rt.headers
data = dict(**rt.headers)
data.update(body)
return data

Expand Down Expand Up @@ -2530,7 +2531,7 @@ def append_object(self, Bucket, Key, Position, Data, **kwargs):
data=Data,
headers=headers,
params=params)
response = rt.headers
response = dict(**rt.headers)
return response

def put_object_from_local_file(self, Bucket, LocalFilePath, Key, EnableMD5=False, **kwargs):
Expand Down
7 changes: 3 additions & 4 deletions qcloud_cos/cos_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def get_md5(data):

def get_content_md5(body):
"""计算任何输入流的md5值"""
body_type = type(body)
if body_type == string_types:
if isinstance(body, string_types):
return get_md5(body)
elif hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
file_position = body.tell() # 记录文件当前位置
Expand Down Expand Up @@ -226,7 +225,7 @@ def format_region(region):
if not region:
raise CosClientError("region is required not empty!")
region = to_unicode(region)
if not re.match('^[A-Za-z0-9][A-Za-z0-9.\-]*[A-Za-z0-9]$', region):
if not re.match(r'^[A-Za-z0-9][A-Za-z0-9.\-]*[A-Za-z0-9]$', region):
raise CosClientError("region format is illegal, only digit, letter and - is allowed!")
if region.find(u'cos.') != -1:
return region # 传入cos.ap-beijing-1这样显示加上cos.的region
Expand Down Expand Up @@ -261,7 +260,7 @@ def format_bucket(bucket, appid):
raise CosClientError("bucket is not string")
if not bucket:
raise CosClientError("bucket is required not empty")
if not (re.match('^[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]$', bucket) or re.match('^[A-Za-z0-9]$', bucket)):
if not (re.match(r'^[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]$', bucket) or re.match('^[A-Za-z0-9]$', bucket)):
raise CosClientError("bucket format is illegal, only digit, letter and - is allowed!")
# appid为空直接返回bucket
if not appid:
Expand Down
2 changes: 2 additions & 0 deletions qcloud_cos/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

__version__ = '5.1.6.2'
14 changes: 12 additions & 2 deletions ut/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ def test_put_object_enable_md5():
with open(file_name, 'rb') as f:
etag = get_raw_md5(f.read())
with open(file_name, 'rb') as fp:
# fp验证
put_response = client.put_object(
Bucket=test_bucket,
Body=fp,
Expand All @@ -766,7 +767,16 @@ def test_put_object_enable_md5():
CacheControl='no-cache',
ContentDisposition='download.txt'
)
assert etag == put_response['Etag']
assert etag == put_response['ETag']
put_response = client.put_object(
Bucket=test_bucket,
Body='TestMD5',
Key=file_name,
EnableMD5=True,
CacheControl='no-cache',
ContentDisposition='download.txt'
)
assert put_response
if os.path.exists(file_name):
os.remove(file_name)

Expand Down Expand Up @@ -877,7 +887,7 @@ def test_put_file_like_object():
Bucket=test_bucket,
Key='test_file_like_object',
Body=input,
EnableMD5=True
EnableMD5=True
)
assert rt

Expand Down