Skip to content

Commit

Permalink
modify upload_file to fetch the last uploadid and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
dt3310321 committed May 2, 2018
1 parent 4c2403b commit ce50c03
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion qcloud_cos/cos_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __call__(self, r):
format_str = u"{method}\n{host}\n{params}\n{headers}\n".format(
method=r.method.lower(),
host=path,
params=urlencode(sorted(uri_params.items())),
params=urlencode(sorted(uri_params.items())).replace('+', '%20'),
headers='&'.join(map(lambda tupl: "%s=%s" % (tupl[0], tupl[1]), sorted(headers.items())))
)
logger.debug("format str: " + format_str)
Expand Down
9 changes: 6 additions & 3 deletions qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1830,9 +1830,12 @@ def _get_resumable_uploadid(self, bucket, key):
Prefix=key
)
if 'Upload' in multipart_response:
if multipart_response['Upload'][0]['Key'] == key:
return multipart_response['Upload'][0]['UploadId']

# 取最后一个(最新的)uploadid
index = len(multipart_response['Upload']) - 1
while index >= 0:
if multipart_response['Upload'][index]['Key'] == key:
return multipart_response['Upload'][index]['UploadId']
index -= 1
return None

def _check_single_upload_part(self, local_path, offset, local_part_size, remote_part_size, remote_etag):
Expand Down
67 changes: 61 additions & 6 deletions qcloud_cos/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import logging

# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7
# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x

# pip安装指南:pip install -U cos-python-sdk-v5

Expand All @@ -32,21 +32,57 @@
Body=fp,
Key=file_name,
StorageClass='STANDARD',
CacheControl='no-cache',
ContentDisposition='download.txt'
ContentType='text/html; charset=utf-8'
)
print(response['ETag'])

# 字节流 简单上传
response = client.put_object(
Bucket='test04-123456789',
Body='abcdefg',
Body=b'abcdefg',
Key=file_name
)
print(response['ETag'])

# 本地路径 简单上传
response = client.put_object_from_local_file(
Bucket='test04-123456789',
LocalFilePath='local.txt',
Key=file_name,
CacheControl='no-cache',
ContentDisposition='download.txt'
)
print(response['ETag'])

# 设置HTTP头部 简单上传
response = client.put_object(
Bucket='test04-123456789',
Body=b'test',
Key=file_name,
ContentType='text/html; charset=utf-8'
)
print(response['ETag'])

# 设置自定义头部 简单上传
response = client.put_object(
Bucket='test04-123456789',
Body=b'test',
Key=file_name,
Metadata={
'x-cos-meta-key1': 'value1',
'x-cos-meta-key2': 'value2'
}
)
print(response['ETag'])

# 高级上传接口(推荐)
response = client.upload_file(
Bucket='test04-123456789',
LocalFilePath='local.txt',
Key=file_name,
PartSize=10,
MAXThread=10
)
print response['ETag']

# 文件下载 获取文件到本地
response = client.get_object(
Bucket='test04-123456789',
Expand All @@ -62,6 +98,25 @@
fp = response['Body'].get_raw_stream()
print(fp.read(2))

# 文件下载 设置Response HTTP 头部
response = client.get_object(
Bucket='test04-123456789',
Key=file_name,
ResponseContentType='text/html; charset=utf-8'
)
print response['Content-Type']
fp = response['Body'].get_raw_stream()
print(fp.read(2))

# 文件下载 指定下载范围
response = client.get_object(
Bucket='test04-123456789',
Key=file_name,
Range='bytes=0-10'
)
fp = response['Body'].get_raw_stream()
print(fp.read())

# 文件下载 捕获异常
try:
response = client.get_object(
Expand Down

0 comments on commit ce50c03

Please sign in to comment.