Skip to content

Commit b80f918

Browse files
committed
Add ut for copy
1 parent 955e6f7 commit b80f918

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

qcloud_cos/cos_comm.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from cos_exception import CosClientError
1313
from cos_exception import CosServiceError
1414

15+
SINGLE_UPLOAD_LENGTH = 5*1024*1024*1024 # 单次上传文件最大为5G
1516
# kwargs中params到http headers的映射
1617
maplist = {
1718
'ContentLength': 'Content-Length',
@@ -165,7 +166,7 @@ def format_bucket(bucket, appid):
165166
if not isinstance(bucket, str):
166167
raise CosClientError("bucket is not str")
167168
# appid为空直接返回bucket
168-
if appid == "":
169+
if not appid:
169170
return bucket
170171
# appid不为空,检查是否以-appid结尾
171172
if bucket.endswith("-"+appid):
@@ -175,7 +176,7 @@ def format_bucket(bucket, appid):
175176

176177
def get_copy_source_info(CopySource):
177178
"""获取拷贝源的所有信息"""
178-
appid = None
179+
appid = ""
179180
if 'Appid' in CopySource.keys():
180181
appid = CopySource['Appid']
181182
if 'Bucket' in CopySource.keys():
@@ -190,8 +191,6 @@ def get_copy_source_info(CopySource):
190191
raise CosClientError('CopySource Need Parameter Region')
191192
if 'Key' in CopySource.keys():
192193
path = CopySource['Key']
193-
if path and path[0] == '/':
194-
path = path[1:]
195194
else:
196195
raise CosClientError('CopySource Need Parameter Key')
197196
return bucket, path, region
@@ -200,9 +199,20 @@ def get_copy_source_info(CopySource):
200199
def gen_copy_source_url(CopySource):
201200
"""拼接拷贝源url"""
202201
bucket, path, region = get_copy_source_info(CopySource)
202+
if path and path[0] == '/':
203+
path = path[1:]
203204
url = "{bucket}.{region}.myqcloud.com/{path}".format(
204205
bucket=bucket,
205206
region=region,
206207
path=path
207208
)
208209
return url
210+
211+
212+
def gen_copy_source_range(begin_range, end_range):
213+
"""拼接bytes=begin-end形式的字符串"""
214+
range = "bytes={first}-{end}".format(
215+
first=begin_range,
216+
end=end_range
217+
)
218+
return range

qcloud_cos/test.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ def test_put_get_delete_object_10MB():
8282
# get object
8383
get_response = client.get_object(
8484
Bucket=test_bucket,
85-
Key=file_name
85+
Key=file_name,
86+
ResponseCacheControl='private'
8687
)
8788
assert etag == get_response['ETag']
89+
assert 'private' == get_response['Cache-Control']
8890
download_fp = get_response['Body'].get_raw_stream()
8991
assert download_fp
9092
# delete object
@@ -494,7 +496,6 @@ def test_put_get_delete_replication():
494496
response = client.get_bucket_replication(
495497
Bucket=test_bucket
496498
)
497-
print response
498499
assert response
499500
# delete lifecycle
500501
response = client.delete_bucket_replication(
@@ -559,11 +560,24 @@ def test_upload_file_multithreading():
559560
os.remove(file_name)
560561
print ed - st
561562

563+
564+
def test_copy_file_automatically():
565+
"""根据拷贝源文件的大小自动选择拷贝策略,小于5G直接copy_object,大于5G分块拷贝"""
566+
copy_source = {'Appid': '1252448703', 'Bucket': 'testbucket', 'Key': '/thread_1MB', 'Region': 'ap-guangzhou'}
567+
response = client.copy(
568+
Bucket=test_bucket,
569+
Key='copy_10G.txt',
570+
CopySource=copy_source,
571+
MAXThread=10
572+
)
573+
574+
562575
if __name__ == "__main__":
563576
setUp()
564577
test_put_get_delete_object_10MB()
565578
test_put_get_versioning()
566579
test_put_get_delete_replication()
567580
test_upload_part_copy()
568581
test_upload_file_multithreading()
582+
test_copy_file_automatically()
569583
tearDown()

0 commit comments

Comments
 (0)