Skip to content

Commit fa0497d

Browse files
committed
Deal with empty file to set Content-Length to 0
1 parent b80f918 commit fa0497d

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

qcloud_cos/cos_client.py

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def put_object(self, Bucket, Body, Key, **kwargs):
180180
logger.info("put object, url=:{url} ,headers=:{headers}".format(
181181
url=url,
182182
headers=headers))
183+
Body = deal_with_empty_file_stream(Body)
183184
rt = self.send_request(
184185
method='PUT',
185186
url=url,
@@ -413,6 +414,7 @@ def upload_part(self, Bucket, Key, Body, PartNumber, UploadId, **kwargs):
413414
logger.info("put object, url=:{url} ,headers=:{headers}".format(
414415
url=url,
415416
headers=headers))
417+
Body = deal_with_empty_file_stream(Body)
416418
rt = self.send_request(
417419
method='PUT',
418420
url=url,

qcloud_cos/cos_comm.py

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hashlib
44
import base64
55
import os
6+
import io
67
import sys
78
import xml.dom.minidom
89
import xml.etree.ElementTree
@@ -216,3 +217,17 @@ def gen_copy_source_range(begin_range, end_range):
216217
end=end_range
217218
)
218219
return range
220+
221+
222+
def deal_with_empty_file_stream(data):
223+
"""对于文件流的剩余长度为0的情况下,返回空字节流"""
224+
if hasattr(data, 'fileno') and hasattr(data, 'tell'):
225+
try:
226+
fileno = data.fileno()
227+
total_length = os.fstat(fileno).st_size
228+
current_position = data.tell()
229+
if total_length - current_position == 0:
230+
return ""
231+
except io.UnsupportedOperation:
232+
return ""
233+
return data

qcloud_cos/test.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def tearDown():
5656

5757
def test_put_get_delete_object_10MB():
5858
"""简单上传下载删除10MB小文件"""
59-
file_size = 10
59+
file_size = 5
6060
file_id = str(random.randint(0, 1000)) + str(random.randint(0, 1000))
6161
file_name = "tmp" + file_id + "_" + str(file_size) + "MB"
6262
gen_file(file_name, 10)
@@ -545,7 +545,7 @@ def test_list_multipart_uploads():
545545
def test_upload_file_multithreading():
546546
"""根据文件大小自动选择分块大小,多线程并发上传提高上传速度"""
547547
file_name = "thread_1GB"
548-
gen_file(file_name, 12) # set 12MB beacuse travis too slow
548+
gen_file(file_name, 5) # set 5MB beacuse travis too slow
549549
st = time.time() # 记录开始时间
550550
response = client.upload_file(
551551
Bucket=test_bucket,
@@ -572,8 +572,24 @@ def test_copy_file_automatically():
572572
)
573573

574574

575+
def test_upload_empty_file():
576+
"""上传一个空文件,不能返回411错误"""
577+
file_name = "empty.txt"
578+
with open(file_name, 'wb') as f:
579+
pass
580+
with open(file_name, 'rb') as fp:
581+
response = client.put_object(
582+
Bucket=test_bucket,
583+
Body=fp,
584+
Key=file_name,
585+
CacheControl='no-cache',
586+
ContentDisposition='download.txt'
587+
)
588+
589+
575590
if __name__ == "__main__":
576591
setUp()
592+
test_upload_empty_file()
577593
test_put_get_delete_object_10MB()
578594
test_put_get_versioning()
579595
test_put_get_delete_replication()

0 commit comments

Comments
 (0)