4
4
import time
5
5
6
6
from qiniu import config
7
- from qiniu .utils import urlsafe_base64_encode , crc32 , file_crc32 , _file_iter , rfc_from_timestamp
7
+ from qiniu .exceptions import UploadException
8
+ from qiniu .utils import urlsafe_base64_encode , crc32 , file_crc32 , _file_iter , rfc_from_timestamp , etag
8
9
from qiniu import http
9
10
from .upload_progress_recorder import UploadProgressRecorder
10
11
@@ -44,7 +45,8 @@ def put_data(
44
45
45
46
def put_file (up_token , key , file_path , params = None ,
46
47
mime_type = 'application/octet-stream' , check_crc = False ,
47
- progress_handler = None , upload_progress_recorder = None , keep_last_modified = False ):
48
+ progress_handler = None , upload_progress_recorder = None ,
49
+ keep_last_modified = False , raise_exception = False ):
48
50
"""上传文件到七牛
49
51
50
52
Args:
@@ -53,9 +55,10 @@ def put_file(up_token, key, file_path, params=None,
53
55
file_path: 上传文件的路径
54
56
params: 自定义变量,规格参考 http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
55
57
mime_type: 上传数据的mimeType
56
- check_crc: 是否校验crc32
58
+ check_crc: 是否校验crc32, 已弃用
57
59
progress_handler: 上传进度
58
60
upload_progress_recorder: 记录上传进度,用于断点续传
61
+ raise_exception: 上传后自动校验key和hash, 如果不一致就报错
59
62
60
63
Returns:
61
64
一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
@@ -77,6 +80,9 @@ def put_file(up_token, key, file_path, params=None,
77
80
ret , info = _form_put (up_token , key , input_stream , params , mime_type ,
78
81
crc , progress_handler , file_name ,
79
82
modify_time = modify_time , keep_last_modified = keep_last_modified )
83
+ if raise_exception is True :
84
+ if (ret ["key" ] != key ) or (ret ["hash" ] != etag (file_path )):
85
+ raise UploadException ("数据校验不正确" )
80
86
return ret , info
81
87
82
88
0 commit comments