Skip to content

Commit 0b8be98

Browse files
committed
Add get_date for lifecycle date and check the object size
1 parent dbcca70 commit 0b8be98

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

qcloud_cos/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from .cos_exception import CosServiceError
44
from .cos_exception import CosClientError
55
from .cos_auth import CosS3Auth
6+
from .cos_comm import get_date

qcloud_cos/cos_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import xml.dom.minidom
1212
import xml.etree.ElementTree
1313
from requests import Request, Session
14+
from datetime import datetime
1415
from urllib import quote
1516
from hashlib import md5
1617
from streambody import StreamBody
@@ -152,7 +153,7 @@ def send_request(self, method, url, timeout=30, **kwargs):
152153
timeout = self._conf._timeout
153154
if self._conf._token is not None:
154155
kwargs['headers']['x-cos-security-token'] = self._conf._token
155-
kwargs['headers']['User-Agent'] = 'cos-python-sdk-v5.1.4.0'
156+
kwargs['headers']['User-Agent'] = 'cos-python-sdk-v5.1.4.1'
156157
try:
157158
for j in range(self._retry):
158159
if method == 'POST':
@@ -214,6 +215,7 @@ def put_object(self, Bucket, Body, Key, EnableMD5=False, **kwargs):
214215
)
215216
print response['ETag']
216217
"""
218+
check_object_content_length(Body)
217219
headers = mapped(kwargs)
218220
if 'Metadata' in headers.keys():
219221
for i in headers['Metadata'].keys():
@@ -579,6 +581,7 @@ def upload_part(self, Bucket, Key, Body, PartNumber, UploadId, EnableMD5=False,
579581
Key='test.txt'
580582
)
581583
"""
584+
check_object_content_length(Body)
582585
headers = mapped(kwargs)
583586
url = self._conf.uri(bucket=Bucket, path=quote(Key, '/-_.~')+"?partNumber={PartNumber}&uploadId={UploadId}".format(
584587
PartNumber=PartNumber,
@@ -1334,7 +1337,7 @@ def put_bucket_lifecycle(self, Bucket, LifecycleConfiguration={}, **kwargs):
13341337
lifecycle_config = {
13351338
'Rule': [
13361339
{
1337-
'Expiration': {'Days': 100},
1340+
'Expiration': {'Date': get_date(2018, 4, 24)},
13381341
'ID': '123',
13391342
'Filter': {'Prefix': ''},
13401343
'Status': 'Enabled',

qcloud_cos/cos_comm.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import xml.dom.minidom
99
import xml.etree.ElementTree
10+
from datetime import datetime
1011
from urllib import quote
1112
from urllib import unquote
1213
from xml2dict import Xml2Dict
@@ -268,6 +269,21 @@ def gen_copy_source_range(begin_range, end_range):
268269
return range
269270

270271

272+
def check_object_content_length(data):
273+
"""put_object接口和upload_part接口的文件大小不允许超过5G"""
274+
content_len = 0
275+
if type(data) is str:
276+
content_len = len(data)
277+
elif type(data) is file and hasattr(data, 'fileno') and hasattr(data, 'tell'):
278+
fileno = data.fileno()
279+
total_length = os.fstat(fileno).st_size
280+
current_position = data.tell()
281+
content_len = total_length - current_position
282+
if content_len > SINGLE_UPLOAD_LENGTH:
283+
raise CosClientError('The object size you upload can not be larger than 5GB in put_object or upload_part')
284+
return None
285+
286+
271287
def deal_with_empty_file_stream(data):
272288
"""对于文件流的剩余长度为0的情况下,返回空字节流"""
273289
if hasattr(data, 'fileno') and hasattr(data, 'tell'):
@@ -304,3 +320,10 @@ def decode_result(data, key_lst, multi_key_list):
304320
if multi_key[1] in item.keys() and item[multi_key[1]]:
305321
item[multi_key[1]] = unquote(item[multi_key[1]])
306322
return data
323+
324+
325+
def get_date(yy, mm, dd):
326+
"""获取lifecycle中Date字段"""
327+
date_str = datetime(yy, mm, dd).isoformat()
328+
final_date_str = date_str+'+08:00'
329+
return final_date_str

ut/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from qcloud_cos import CosS3Client
99
from qcloud_cos import CosConfig
1010
from qcloud_cos import CosServiceError
11+
from qcloud_cos import get_date
1112

1213
SECRET_ID = os.environ["SECRET_ID"]
1314
SECRET_KEY = os.environ["SECRET_KEY"]
@@ -429,7 +430,7 @@ def test_put_get_delete_lifecycle():
429430
lifecycle_config = {
430431
'Rule': [
431432
{
432-
'Expiration': {'Days': 100},
433+
'Expiration': {'Date': get_date(2030, 5, 1)},
433434
'ID': '123',
434435
'Filter': {'Prefix': ''},
435436
'Status': 'Enabled',

0 commit comments

Comments
 (0)