Skip to content

Commit b5a4b7c

Browse files
authored
Merge pull request #77 from dt3310321/s3
S3
2 parents e1ede44 + fdad120 commit b5a4b7c

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

Diff for: .travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ install:
99
- pip install requests
1010
- pip install six
1111
- pip install nose
12-
- pip install pep8
12+
- pip install pycodestyle
1313
- pip install dicttoxml
1414
script:
15-
- pep8 --max-line-length=180 qcloud_cos/.
15+
- pycodestyle --max-line-length=180 qcloud_cos/.
1616
- nosetests -s -v ut/
1717
deploy:
1818
provider: pypi

Diff for: qcloud_cos/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,14 @@
44
from .cos_exception import CosClientError
55
from .cos_auth import CosS3Auth
66
from .cos_comm import get_date
7+
8+
import logging
9+
10+
try:
11+
from logging import NullHandler
12+
except ImportError:
13+
class NullHandler(logging.Handler):
14+
def emit(self, record):
15+
pass
16+
17+
logging.getLogger(__name__).addHandler(NullHandler())

Diff for: qcloud_cos/cos_client.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .cos_threadpool import SimpleThreadPool
2323
from .cos_exception import CosClientError
2424
from .cos_exception import CosServiceError
25+
from .version import __version__
2526

2627
logger = logging.getLogger(__name__)
2728

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

284-
response = rt.headers
285+
response = dict(**rt.headers)
285286
return response
286287

287288
def get_object(self, Bucket, Key, **kwargs):
@@ -332,7 +333,7 @@ def get_object(self, Bucket, Key, **kwargs):
332333
params=params,
333334
headers=headers)
334335

335-
response = rt.headers
336+
response = dict(**rt.headers)
336337
response['Body'] = StreamBody(rt)
337338

338339
return response
@@ -422,7 +423,7 @@ def delete_object(self, Bucket, Key, **kwargs):
422423
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key),
423424
headers=headers,
424425
params=params)
425-
data = rt.headers
426+
data = dict(**rt.headers)
426427
return data
427428

428429
def delete_objects(self, Bucket, Delete={}, **kwargs):
@@ -511,7 +512,7 @@ def head_object(self, Bucket, Key, **kwargs):
511512
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key, params=params),
512513
headers=headers,
513514
params=params)
514-
return rt.headers
515+
return dict(**rt.headers)
515516

516517
def copy_object(self, Bucket, Key, CopySource, CopyStatus='Copy', **kwargs):
517518
"""文件拷贝,文件信息修改
@@ -554,7 +555,7 @@ def copy_object(self, Bucket, Key, CopySource, CopyStatus='Copy', **kwargs):
554555
if 'ETag' not in body:
555556
logger.error(rt.content)
556557
raise CosServiceError('PUT', rt.content, 200)
557-
data = rt.headers
558+
data = dict(**rt.headers)
558559
data.update(body)
559560
return data
560561

@@ -601,7 +602,7 @@ def upload_part_copy(self, Bucket, Key, PartNumber, UploadId, CopySource, CopySo
601602
params=params,
602603
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key, params=params))
603604
body = xml_to_dict(rt.content)
604-
data = rt.headers
605+
data = dict(**rt.headers)
605606
data.update(body)
606607
return data
607608

@@ -734,7 +735,7 @@ def complete_multipart_upload(self, Bucket, Key, UploadId, MultipartUpload={}, *
734735
if 'ETag' not in body:
735736
logger.error(rt.content)
736737
raise CosServiceError('POST', rt.content, 200)
737-
data = rt.headers
738+
data = dict(**rt.headers)
738739
data.update(body)
739740
return data
740741

@@ -2530,7 +2531,7 @@ def append_object(self, Bucket, Key, Position, Data, **kwargs):
25302531
data=Data,
25312532
headers=headers,
25322533
params=params)
2533-
response = rt.headers
2534+
response = dict(**rt.headers)
25342535
return response
25352536

25362537
def put_object_from_local_file(self, Bucket, LocalFilePath, Key, EnableMD5=False, **kwargs):

Diff for: qcloud_cos/cos_comm.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def get_md5(data):
9898

9999
def get_content_md5(body):
100100
"""计算任何输入流的md5值"""
101-
body_type = type(body)
102-
if body_type == string_types:
101+
if isinstance(body, string_types):
103102
return get_md5(body)
104103
elif hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
105104
file_position = body.tell() # 记录文件当前位置
@@ -226,7 +225,7 @@ def format_region(region):
226225
if not region:
227226
raise CosClientError("region is required not empty!")
228227
region = to_unicode(region)
229-
if not re.match('^[A-Za-z0-9][A-Za-z0-9.\-]*[A-Za-z0-9]$', region):
228+
if not re.match(r'^[A-Za-z0-9][A-Za-z0-9.\-]*[A-Za-z0-9]$', region):
230229
raise CosClientError("region format is illegal, only digit, letter and - is allowed!")
231230
if region.find(u'cos.') != -1:
232231
return region # 传入cos.ap-beijing-1这样显示加上cos.的region
@@ -261,7 +260,7 @@ def format_bucket(bucket, appid):
261260
raise CosClientError("bucket is not string")
262261
if not bucket:
263262
raise CosClientError("bucket is required not empty")
264-
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)):
263+
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)):
265264
raise CosClientError("bucket format is illegal, only digit, letter and - is allowed!")
266265
# appid为空直接返回bucket
267266
if not appid:

Diff for: qcloud_cos/version.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
__version__ = '5.1.6.2'

Diff for: ut/test.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ def test_put_object_enable_md5():
758758
with open(file_name, 'rb') as f:
759759
etag = get_raw_md5(f.read())
760760
with open(file_name, 'rb') as fp:
761+
# fp验证
761762
put_response = client.put_object(
762763
Bucket=test_bucket,
763764
Body=fp,
@@ -766,7 +767,16 @@ def test_put_object_enable_md5():
766767
CacheControl='no-cache',
767768
ContentDisposition='download.txt'
768769
)
769-
assert etag == put_response['Etag']
770+
assert etag == put_response['ETag']
771+
put_response = client.put_object(
772+
Bucket=test_bucket,
773+
Body='TestMD5',
774+
Key=file_name,
775+
EnableMD5=True,
776+
CacheControl='no-cache',
777+
ContentDisposition='download.txt'
778+
)
779+
assert put_response
770780
if os.path.exists(file_name):
771781
os.remove(file_name)
772782

@@ -877,7 +887,7 @@ def test_put_file_like_object():
877887
Bucket=test_bucket,
878888
Key='test_file_like_object',
879889
Body=input,
880-
EnableMD5=True
890+
EnableMD5=True
881891
)
882892
assert rt
883893

0 commit comments

Comments
 (0)