|
12 | 12 | import xml.etree.ElementTree
|
13 | 13 | from requests import Request, Session
|
14 | 14 | from urllib import quote
|
| 15 | +from hashlib import md5 |
15 | 16 | from streambody import StreamBody
|
16 | 17 | from xml2dict import Xml2Dict
|
17 | 18 | from dicttoxml import dicttoxml
|
@@ -49,7 +50,7 @@ def __init__(self, Appid=None, Region=None, Secret_id=None, Secret_key=None, Tok
|
49 | 50 | if Scheme is None:
|
50 | 51 | Scheme = 'http'
|
51 | 52 | if(Scheme != 'http' and Scheme != 'https'):
|
52 |
| - raise CosCosClientError('Scheme can be only set to http/https') |
| 53 | + raise CosClientError('Scheme can be only set to http/https') |
53 | 54 | self._scheme = Scheme
|
54 | 55 |
|
55 | 56 | # 兼容(SecretId,SecretKey)以及(AccessId,AccessKey)
|
@@ -597,6 +598,10 @@ def upload_part(self, Bucket, Key, Body, PartNumber, UploadId, EnableMD5=False,
|
597 | 598 | auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key),
|
598 | 599 | data=Body)
|
599 | 600 | response = dict()
|
| 601 | + logger.debug("local md5: {key}".format(key=rt.headers['ETag'][1:-1])) |
| 602 | + logger.debug("cos md5: {key}".format(key=md5(Body).hexdigest())) |
| 603 | + if md5(Body).hexdigest() != rt.headers['ETag'][1:-1]: |
| 604 | + raise CosClientError("MD5 inconsistencies") |
600 | 605 | response['ETag'] = rt.headers['ETag']
|
601 | 606 | return response
|
602 | 607 |
|
@@ -2011,5 +2016,36 @@ def upload_file_from_buffer(self, Bucket, Key, Body, MaxBufferSize=100, PartSize
|
2011 | 2016 | raise e
|
2012 | 2017 | return rt
|
2013 | 2018 |
|
| 2019 | + def append_object(self, Bucket, Key, Position, Data, **kwargs): |
| 2020 | + """文件块追加接口 |
| 2021 | +
|
| 2022 | + :param Bucket(string): 存储桶名称. |
| 2023 | + :param Key(string): COS路径. |
| 2024 | + :param Position(int): 追加内容的起始位置. |
| 2025 | + :param Data(string): 追加的内容 |
| 2026 | + :kwargs(dict): 设置上传的headers. |
| 2027 | + :return(dict): 上传成功返回的结果,包含ETag等信息. |
| 2028 | + """ |
| 2029 | + headers = mapped(kwargs) |
| 2030 | + if 'Metadata' in headers.keys(): |
| 2031 | + for i in headers['Metadata'].keys(): |
| 2032 | + headers[i] = headers['Metadata'][i] |
| 2033 | + headers.pop('Metadata') |
| 2034 | + |
| 2035 | + url = self._conf.uri(bucket=Bucket, path=quote(Key, '/-_.~')+"?append&position="+str(Position)) |
| 2036 | + logger.info("append object, url=:{url} ,headers=:{headers}".format( |
| 2037 | + url=url, |
| 2038 | + headers=headers)) |
| 2039 | + Body = deal_with_empty_file_stream(Data) |
| 2040 | + rt = self.send_request( |
| 2041 | + method='POST', |
| 2042 | + url=url, |
| 2043 | + auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key), |
| 2044 | + data=Body, |
| 2045 | + headers=headers) |
| 2046 | + response = rt.headers |
| 2047 | + return response |
| 2048 | + |
| 2049 | + |
2014 | 2050 | if __name__ == "__main__":
|
2015 | 2051 | pass
|
0 commit comments