Skip to content

Commit 7a0e678

Browse files
authored
Merge pull request #70 from dt3310321/s3
S3
2 parents 1221df0 + 945d11d commit 7a0e678

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

qcloud_cos/cos_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ def copy_object(self, Bucket, Key, CopySource, CopyStatus='Copy', **kwargs):
515515
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key),
516516
headers=headers)
517517
body = xml_to_dict(rt.content)
518+
if 'ETag' not in body:
519+
logger.error(rt.content)
520+
raise CosServiceError('PUT', rt.content, 200)
518521
data = rt.headers
519522
data.update(body)
520523
return data
@@ -692,6 +695,10 @@ def complete_multipart_upload(self, Bucket, Key, UploadId, MultipartUpload={}, *
692695
headers=headers,
693696
params=params)
694697
body = xml_to_dict(rt.content)
698+
# 分块上传文件返回200OK并不能代表文件上传成功,返回的body里面如果没有ETag则认为上传失败
699+
if 'ETag' not in body:
700+
logger.error(rt.content)
701+
raise CosServiceError('POST', rt.content, 200)
695702
data = rt.headers
696703
data.update(body)
697704
return data

qcloud_cos/streambody.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ def get_stream(self, chunk_size=1024):
1212
return self._rt.iter_content(chunk_size=chunk_size)
1313

1414
def get_stream_to_file(self, file_name):
15+
use_chunked = False
1516
if 'Content-Length' in self._rt.headers:
1617
content_len = int(self._rt.headers['Content-Length'])
18+
elif 'Transfer-Encoding' in self._rt.headers and self._rt.headers['Transfer-Encoding'] == "chunked":
19+
use_chunked = True
1720
else:
18-
raise IOError("download failed without Content-Length header")
21+
raise IOError("download failed without Content-Length header or Transfer-Encoding header")
1922

2023
file_len = 0
2124
with open(file_name, 'wb') as fp:
2225
for chunk in self._rt.iter_content(chunk_size=1024):
2326
if chunk:
2427
file_len += len(chunk)
2528
fp.write(chunk)
26-
fp.flush()
27-
fp.close()
28-
if file_len != content_len:
29+
if not use_chunked and file_len != content_len:
2930
raise IOError("download failed with incomplete file")

0 commit comments

Comments
 (0)