5
5
import time
6
6
7
7
from qiniu import config , Auth
8
- from qiniu .utils import urlsafe_base64_encode , crc32 , file_crc32 , _file_iter , rfc_from_timestamp
8
+ from qiniu import config
9
+ from qiniu .exceptions import UploadException
10
+ from qiniu .utils import urlsafe_base64_encode , crc32 , file_crc32 , _file_iter , rfc_from_timestamp , etag
9
11
from qiniu import http
10
12
from .upload_progress_recorder import UploadProgressRecorder
11
13
@@ -47,6 +49,7 @@ def put_data(
47
49
def put_file (up_token , key , file_path , params = None ,
48
50
mime_type = 'application/octet-stream' , check_crc = False ,
49
51
progress_handler = None , upload_progress_recorder = None , keep_last_modified = False , hostscache_dir = None ,
52
+ raise_exception = False ,
50
53
part_size = None , version = None , bucket_name = None ):
51
54
"""上传文件到七牛
52
55
@@ -63,6 +66,7 @@ def put_file(up_token, key, file_path, params=None,
63
66
version 分片上传版本 目前支持v1/v2版本 默认v1
64
67
part_size 分片上传v2必传字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
65
68
bucket_name 分片上传v2字段 空间名称
69
+ raise_exception: 上传后自动校验key和hash, 如果不一致就报错
66
70
67
71
Returns:
68
72
一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
@@ -84,6 +88,9 @@ def put_file(up_token, key, file_path, params=None,
84
88
ret , info = _form_put (up_token , key , input_stream , params , mime_type ,
85
89
crc , hostscache_dir , progress_handler , file_name ,
86
90
modify_time = modify_time , keep_last_modified = keep_last_modified )
91
+ if raise_exception is True :
92
+ if (ret ["key" ] != key ) or (ret ["hash" ] != etag (file_path )):
93
+ raise UploadException ("数据校验不正确" )
87
94
return ret , info
88
95
89
96
0 commit comments