Skip to content

Commit 0673b59

Browse files
committed
Add demo for upload/download
1 parent 01ba795 commit 0673b59

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

qcloud_cos/demo.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding=utf-8
2+
from qcloud_cos import CosConfig
3+
from qcloud_cos import CosS3Client
4+
5+
# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7
6+
7+
# pip安装指南:pip install -U cos-python-sdk-v5
8+
9+
# cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224
10+
11+
# 设置用户属性, 包括appid, secret_id, secret_key, region
12+
appid = '1242338703' # 替换为用户的appid
13+
secret_id = 'AKID15IsskiBQACGbAo6WhgcQbVls7HmuG00' # 替换为用户的secret_id
14+
secret_key = 'csivKvxxrMvSvQpMWHuIz12pThQQlWRW' # 替换为用户的secret_key
15+
region = 'ap-beijing-1' # 替换为用户的region
16+
token = '' # 使用临时秘钥需要传入Token,默认为空,可不填
17+
config = CosConfig(Appid=appid, Region=region, Access_id=secret_id, Access_key=secret_key, Token=token) # 获取配置对象
18+
client = CosS3Client(config)
19+
20+
# 文件流 简单上传
21+
fp = open('test.txt', 'rb')
22+
file_name = 'test.txt'
23+
response = client.put_object(
24+
Bucket='test04',
25+
Body=fp,
26+
Key=file_name,
27+
StorageClass='STANDARD',
28+
CacheControl='no-cache',
29+
ContentDisposition='download.txt'
30+
)
31+
fp.close()
32+
print response['ETag']
33+
34+
# 字节流 简单上传
35+
response = client.put_object(
36+
Bucket='test04',
37+
Body='abcdefg',
38+
Key=file_name,
39+
CacheControl='no-cache',
40+
ContentDisposition='download.txt'
41+
)
42+
print response['ETag']
43+
44+
# 文件下载 获取文件到本地
45+
response = client.get_object(
46+
Bucket='test04',
47+
Key=file_name,
48+
)
49+
response['Body'].get_stream_to_file('output.txt')
50+
51+
# 文件下载 获取文件流
52+
response = client.get_object(
53+
Bucket='test04',
54+
Key=file_name,
55+
)
56+
fp = response['Body'].get_raw_stream()
57+
print fp.read(2)

0 commit comments

Comments
 (0)