Skip to content

Commit 961b7e6

Browse files
v2.8.7
1 parent ab42153 commit 961b7e6

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
TOS SDK for Python 版本记录
22
===========================
3+
Version 2.8.7
4+
-------------
5+
- 修改:对Content-Encoding:gzip的数据不进行自动解压
36
Version 2.8.6
47
-------------
58
- 新增:支持桶清单相关接口

tests/test_v2_object.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,18 @@ def test_get_object_range(self):
14951495
out_2 = self.client.get_object(bucket_name, key, range='bytes=1024-')
14961496
self.assertEqual(out_2.read(), content[1024:])
14971497

1498+
def test_get_object_gzip(self):
1499+
bucket_name = self.bucket_name + 'object-gzip'
1500+
self.client.create_bucket(bucket_name)
1501+
self.bucket_delete.append(bucket_name)
1502+
key = self.random_key('.zp')
1503+
content = random_bytes(1024 * 1024)
1504+
self.client.put_object(bucket_name, key, content=content,content_encoding="gzip")
1505+
out = self.client.get_object(bucket_name, key, range='bytes=0-1023')
1506+
self.assertEqual(out.read(), content[0:1024])
1507+
out_2 = self.client.get_object(bucket_name, key, range='bytes=1024-')
1508+
self.assertEqual(out_2.read(), content[1024:])
1509+
14981510
def test_copy_object_range(self):
14991511
src_bucket_name = self.bucket_name + 'copy-object-range'
15001512
key = self.random_key('.js')

tos/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = 'v2.8.6'
1+
__version__ = 'v2.8.7'

tos/clientv2.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ def __init__(self, ak='', sk='', endpoint='', region='',
905905
enable_crc=True,
906906
connection_time=10,
907907
enable_verify_ssl=True,
908-
dns_cache_time=0,
908+
dns_cache_time=15,
909909
proxy_host: str = None,
910910
proxy_port: int = None,
911911
proxy_username: str = None,
@@ -936,7 +936,7 @@ def __init__(self, ak='', sk='', endpoint='', region='',
936936
:param max_connections: 连接池中允许打开的最大 HTTP 连接数,默认 1024
937937
:param enable_crc: 是否开启上传后客户端 CRC 校验,默认为 true
938938
:param enable_verify_ssl: 是否开启 SSL 证书校验,默认为 true
939-
:param dns_cache_time: DNS 缓存的有效期,单位:分钟,小于等于 0 时代表关闭 DNS 缓存,默认为 0
939+
:param dns_cache_time: DNS 缓存的有效期,单位:分钟,小于等于 0 时代表关闭 DNS 缓存,默认为 15
940940
:param proxy_host: 代理服务器的主机地址,当前只支持 http 协议
941941
:param proxy_port: 代理服务器的端口
942942
:param proxy_username: 连接代理服务器时使用的用户名
@@ -2238,10 +2238,13 @@ def get_object_to_file(self, bucket: str, key: str, file_path: str,
22382238
if os.path.isdir(file_path):
22392239
file_path = os.path.join(file_path, key)
22402240
try_make_file_dir(file_path)
2241+
tmp_file_path = file_path + ".temp."+str(uuid.uuid4())
22412242

2242-
with open(file_path, 'wb') as f:
2243+
with open(tmp_file_path, 'wb') as f:
22432244
shutil.copyfileobj(result, f)
22442245

2246+
os.rename(tmp_file_path, file_path)
2247+
22452248
return result
22462249

22472250
def create_multipart_upload(self, bucket: str, key: str,

tos/http.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(self, resp):
3434
self.request_id = self.headers.get('x-tos-request-id', '')
3535
self._all_read = False
3636
self.offset = 0
37+
self.content_encoding = resp.headers.get('content-encoding',None)
3738

3839
def __iter__(self):
3940
return self
@@ -54,8 +55,12 @@ def read(self, amt=None):
5455

5556
if amt is None:
5657
content_list = []
57-
for chunk in self.resp.iter_content(CHUNK_SIZE):
58-
content_list.append(chunk)
58+
if self.content_encoding and hasattr(self.resp.raw, "stream"):
59+
for chunk in self.resp.raw.stream(CHUNK_SIZE, decode_content=False):
60+
content_list.append(chunk)
61+
else:
62+
for chunk in self.resp.iter_content(CHUNK_SIZE):
63+
content_list.append(chunk)
5964
content = b''.join(content_list)
6065

6166
self._all_read = True
@@ -64,7 +69,10 @@ def read(self, amt=None):
6469
return content
6570
else:
6671
try:
67-
read = next(self.resp.iter_content(amt))
72+
if self.content_encoding and hasattr(self.resp.raw, "stream"):
73+
read = next(self.resp.raw.stream(amt, decode_content=False))
74+
else:
75+
read = next(self.resp.iter_content(amt))
6876
self.offset += len(read)
6977
return read
7078
except StopIteration:

0 commit comments

Comments
 (0)