Skip to content

Commit 4230470

Browse files
authored
Merge branch 'master' into s3
2 parents fb79587 + 1fa7e6a commit 4230470

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

README.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224
2727
.. code:: python
2828
2929
# 设置用户属性, 包括secret_id, secret_key, region
30+
# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
3031
secret_id = 'xxxxxxxx' # 替换为用户的secret_id
3132
secret_key = 'xxxxxxx' # 替换为用户的secret_key
32-
  region = 'ap-beiging-1'   # 替换为用户的region
33+
   region = 'ap-beijing-1'   # 替换为用户的region
3334
token = '' # 使用临时秘钥需要传入Token,默认为空,可不填
3435
config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) #获取配置对象
3536
client = CosS3Client(config) #获取客户端对象
@@ -40,7 +41,7 @@ cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224
4041
############################################################################
4142
# 1. 上传单个文件
4243
response = client.put_object(
43-
Bucket='test01-123456789',
44+
       Bucket='test01-123456789',  # Bucket由bucketname-appid组成
4445
Body='TY'*1024*512*file_size,
4546
Key=file_name,
4647
CacheControl='no-cache',

qcloud_cos/cos_client.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import xml.etree.ElementTree
1313
from requests import Request, Session
1414
from urllib import quote
15+
from hashlib import md5
1516
from streambody import StreamBody
1617
from xml2dict import Xml2Dict
1718
from dicttoxml import dicttoxml
@@ -49,7 +50,7 @@ def __init__(self, Appid=None, Region=None, Secret_id=None, Secret_key=None, Tok
4950
if Scheme is None:
5051
Scheme = 'http'
5152
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')
5354
self._scheme = Scheme
5455

5556
# 兼容(SecretId,SecretKey)以及(AccessId,AccessKey)
@@ -597,6 +598,10 @@ def upload_part(self, Bucket, Key, Body, PartNumber, UploadId, EnableMD5=False,
597598
auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key, Key),
598599
data=Body)
599600
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")
600605
response['ETag'] = rt.headers['ETag']
601606
return response
602607

@@ -2011,5 +2016,36 @@ def upload_file_from_buffer(self, Bucket, Key, Body, MaxBufferSize=100, PartSize
20112016
raise e
20122017
return rt
20132018

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+
20142050
if __name__ == "__main__":
20152051
pass

qcloud_cos/demo.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
1616

1717
# 设置用户属性, 包括secret_id, secret_key, region
18+
# appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
1819
secret_id = 'AKID15IsskiBQACGbAo6WhgcQbVls7HmuG00' # 替换为用户的secret_id
1920
secret_key = 'csivKvxxrMvSvQpMWHuIz12pThQQlWRW' # 替换为用户的secret_key
2021
region = 'ap-beijing-1' # 替换为用户的region
@@ -26,7 +27,7 @@
2627
file_name = 'test.txt'
2728
with open('test.txt', 'rb') as fp:
2829
response = client.put_object(
29-
Bucket='test04-123456789',
30+
Bucket='test04-123456789', # Bucket由bucketname-appid组成
3031
Body=fp,
3132
Key=file_name,
3233
StorageClass='STANDARD',

0 commit comments

Comments
 (0)