Skip to content

Commit c95c303

Browse files
authored
Merge pull request #284 from jemygraw/master
add keep_last_modifed to put_file to support some customers' needs
2 parents 1a92915 + f7ec17f commit c95c303

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 7.1.6 (2017-09-26)
4+
5+
*`put_file` 功能增加保持本地文件Last Modified功能,以支持切换源站的客户CDN不回源
6+
7+
## 7.1.5 (2017-08-26)
8+
9+
* 设置表单上传默认校验crc32
10+
* 增加PutPolicy新参数isPrefixalScope
11+
* 修复手动指定的zone无效的问题
12+
313
## 7.1.4 (2017-06-05)
414
### 修正
515
* cdn功能中获取域名日志列表的参数错误

qiniu/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# flake8: noqa
1111

12-
__version__ = '7.1.5'
12+
__version__ = '7.1.6'
1313

1414
from .auth import Auth, QiniuMacAuth
1515

qiniu/services/storage/uploader.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55

66
from qiniu import config
7-
from qiniu.utils import urlsafe_base64_encode, crc32, file_crc32, _file_iter
7+
from qiniu.utils import urlsafe_base64_encode, crc32, file_crc32, _file_iter, rfc_from_timestamp
88
from qiniu import http
99
from .upload_progress_recorder import UploadProgressRecorder
1010

@@ -33,7 +33,7 @@ def put_data(
3333

3434
def put_file(up_token, key, file_path, params=None,
3535
mime_type='application/octet-stream', check_crc=False,
36-
progress_handler=None, upload_progress_recorder=None):
36+
progress_handler=None, upload_progress_recorder=None, keep_last_modified=False):
3737
"""上传文件到七牛
3838
3939
Args:
@@ -55,19 +55,22 @@ def put_file(up_token, key, file_path, params=None,
5555
# fname = os.path.basename(file_path)
5656
with open(file_path, 'rb') as input_stream:
5757
file_name = os.path.basename(file_path)
58+
modify_time = int(os.path.getmtime(file_path))
5859
if size > config._BLOCK_SIZE * 2:
5960
ret, info = put_stream(up_token, key, input_stream, file_name, size, params,
6061
mime_type, progress_handler,
6162
upload_progress_recorder=upload_progress_recorder,
62-
modify_time=(int)(os.path.getmtime(file_path)))
63+
modify_time=modify_time, keep_last_modified=keep_last_modified)
6364
else:
6465
crc = file_crc32(file_path)
65-
ret, info = _form_put(up_token, key, input_stream, params, mime_type, crc, progress_handler, file_name)
66-
# ret, info = _form_put(up_token, key, input_stream, params, mime_type, crc, progress_handler)
66+
ret, info = _form_put(up_token, key, input_stream, params, mime_type,
67+
crc, progress_handler, file_name,
68+
modify_time=modify_time, keep_last_modified=keep_last_modified)
6769
return ret, info
6870

6971

70-
def _form_put(up_token, key, data, params, mime_type, crc, progress_handler=None, file_name=None):
72+
def _form_put(up_token, key, data, params, mime_type, crc, progress_handler=None, file_name=None, modify_time=None,
73+
keep_last_modified=False):
7174
fields = {}
7275
if params:
7376
for k, v in params.items():
@@ -85,6 +88,10 @@ def _form_put(up_token, key, data, params, mime_type, crc, progress_handler=None
8588
if not fname or not fname.strip():
8689
fname = 'file_name'
8790

91+
# last modify time
92+
if modify_time and keep_last_modified:
93+
fields['x-qn-meta-!Last-Modified'] = rfc_from_timestamp(modify_time)
94+
8895
r, info = http._post_file(url, data=fields, files={'file': (fname, data, mime_type)})
8996
if r is None and info.need_retry():
9097
if info.connect_failed:
@@ -102,9 +109,9 @@ def _form_put(up_token, key, data, params, mime_type, crc, progress_handler=None
102109

103110
def put_stream(up_token, key, input_stream, file_name, data_size, params=None,
104111
mime_type=None, progress_handler=None,
105-
upload_progress_recorder=None, modify_time=None):
112+
upload_progress_recorder=None, modify_time=None, keep_last_modified=False):
106113
task = _Resume(up_token, key, input_stream, data_size, params, mime_type,
107-
progress_handler, upload_progress_recorder, modify_time, file_name)
114+
progress_handler, upload_progress_recorder, modify_time, file_name, keep_last_modified)
108115
return task.upload()
109116

110117

@@ -128,7 +135,7 @@ class _Resume(object):
128135
"""
129136

130137
def __init__(self, up_token, key, input_stream, data_size, params, mime_type,
131-
progress_handler, upload_progress_recorder, modify_time, file_name):
138+
progress_handler, upload_progress_recorder, modify_time, file_name, keep_last_modified):
132139
"""初始化断点续上传"""
133140
self.up_token = up_token
134141
self.key = key
@@ -140,6 +147,7 @@ def __init__(self, up_token, key, input_stream, data_size, params, mime_type,
140147
self.upload_progress_recorder = upload_progress_recorder or UploadProgressRecorder()
141148
self.modify_time = modify_time or time.time()
142149
self.file_name = file_name
150+
self.keep_last_modified = keep_last_modified
143151
# print(self.modify_time)
144152
# print(modify_time)
145153

@@ -216,6 +224,11 @@ def file_url(self, host):
216224
for k, v in self.params.items():
217225
url.append('{0}/{1}'.format(k, urlsafe_base64_encode(v)))
218226
pass
227+
228+
if self.modify_time and self.keep_last_modified:
229+
url.append(
230+
"x-qn-meta-!Last-Modified/{0}".format(urlsafe_base64_encode(rfc_from_timestamp(self.modify_time))))
231+
219232
url = '/'.join(url)
220233
# print url
221234
return url

qiniu/utils.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from hashlib import sha1
44
from base64 import urlsafe_b64encode, urlsafe_b64decode
5-
5+
from datetime import datetime
66
from .compat import b, s
77

88
try:
99
import zlib
10+
1011
binascii = zlib
1112
except ImportError:
1213
zlib = None
@@ -158,3 +159,14 @@ def entry(bucket, key):
158159
return urlsafe_base64_encode('{0}'.format(bucket))
159160
else:
160161
return urlsafe_base64_encode('{0}:{1}'.format(bucket, key))
162+
163+
164+
def rfc_from_timestamp(timestamp):
165+
"""将时间戳转换为HTTP RFC格式
166+
167+
Args:
168+
timestamp: 整型Unix时间戳(单位秒)
169+
"""
170+
last_modified_date = datetime.fromtimestamp(timestamp)
171+
last_modified_str = last_modified_date.strftime('%a, %d %b %Y %H:%M:%S GMT')
172+
return last_modified_str

0 commit comments

Comments
 (0)