对象存储

关于腾讯云对象存储的一些玩法

python版本SDK

安装依赖

1
pip install -U cos-python-sdk-v5

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging

logging.basicConfig(level=logging.INFO, stream=sys.stdout)

secret_id = 'secretId'
secret_key = 'secretKey'
region = 'ap-shanghai'
token = None
scheme = 'https'
config = CosConfig(Region=region, SecretId=secret_id,
SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

chunk文件转存

1
2
3
4
5
6
7
8
9
10
11
stream = requests.get(
'https://img.zcool.cn/community/01ab0a5c88b0b7a8012141683b56fc.jpg@520w_390h_1c_1e_1o_100sh.jpg')

# 网络流将以 Transfer-Encoding:chunked 的方式传输到 COS
response = client.put_object(
Bucket='browniu-dbc-1257187612',
Body=stream,
Key='test.png'
)

print('save success,', response['ETag'])

相关文档