Skip to content

Commit 5e9482b

Browse files
committed
上传文件时,可以添加校验参数强制校验key和hash
1 parent 8f2fffd commit 5e9482b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

qiniu/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: UTF-8 -*-
3+
# Xiang Wang @ 2019-09-17 15:25:42
4+
5+
6+
class QiniuException(Exception):
7+
pass
8+
9+
10+
class UploadException(QiniuException):
11+
pass

qiniu/services/storage/uploader.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import time
66

77
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
911
from qiniu import http
1012
from .upload_progress_recorder import UploadProgressRecorder
1113

@@ -47,6 +49,7 @@ def put_data(
4749
def put_file(up_token, key, file_path, params=None,
4850
mime_type='application/octet-stream', check_crc=False,
4951
progress_handler=None, upload_progress_recorder=None, keep_last_modified=False, hostscache_dir=None,
52+
raise_exception=False,
5053
part_size=None, version=None, bucket_name=None):
5154
"""上传文件到七牛
5255
@@ -63,6 +66,7 @@ def put_file(up_token, key, file_path, params=None,
6366
version 分片上传版本 目前支持v1/v2版本 默认v1
6467
part_size 分片上传v2必传字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
6568
bucket_name 分片上传v2字段 空间名称
69+
raise_exception: 上传后自动校验key和hash, 如果不一致就报错
6670
6771
Returns:
6872
一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
@@ -84,6 +88,9 @@ def put_file(up_token, key, file_path, params=None,
8488
ret, info = _form_put(up_token, key, input_stream, params, mime_type,
8589
crc, hostscache_dir, progress_handler, file_name,
8690
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("数据校验不正确")
8794
return ret, info
8895

8996

0 commit comments

Comments
 (0)