1+ # -*- coding=utf-8
2+ from qcloud_cos import CosConfig
3+ from qcloud_cos import CosS3Client
4+ from qcloud_cos .cos_encryption_client import CosEncryptionClient
5+ from qcloud_cos .crypto import AESProvider , RSAKeyPair
6+ import sys
7+ import os
8+ import logging
9+
10+ # 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
11+ logging .basicConfig (level = logging .INFO , stream = sys .stdout )
12+
13+ # 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
14+ secret_id = os .environ ['COS_SECRET_ID' ] # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
15+ secret_key = os .environ ['COS_SECRET_KEY' ] # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
16+ region = 'ap-beijing' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
17+ # COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
18+ token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
19+
20+ conf = CosConfig (Region = region , SecretId = secret_id , SecretKey = secret_key , Token = token )
21+
22+ '''使用对称 AES256 加密每次生成的随机密钥示例
23+ '''
24+
25+ # 方式一:通过密钥值初始化加密客户端
26+ # 注意:按照 AES 算法的要求,aes_key_value 需为 base64编码后的结果
27+ aes_provider = AESProvider (aes_key = 'aes_key_value' )
28+
29+ # 方式二:通过密钥路径初始化加密客户端
30+ aes_key_pair = AESProvider (aes_key_path = 'aes_key_path' )
31+
32+ client_for_aes = CosEncryptionClient (conf , aes_provider )
33+
34+ # 上传对象,兼容非加密客户端的 put_object 的所有功能,具体使用可参考 put_object
35+ response = client_for_aes .put_object (
36+ Bucket = 'examplebucket-1250000000' ,
37+ Body = b'bytes' ,
38+ Key = 'exampleobject' ,
39+ EnableMD5 = False )
40+
41+ # 下载对象,兼容非加密客户端的 get_object 的所有功能,具体使用可参考 get_object
42+ response = client_for_aes .get_object (
43+ Bucket = 'examplebucket-1250000000' ,
44+ Key = 'exampleobject' )
45+
46+ # 分块上传,兼容非加密客户端的分块上传,除了最后一个part,每个 part 的大小必须为16字节的整数倍
47+ response = client_for_aes .create_multipart_upload (
48+ Bucket = 'examplebucket-1250000000' ,
49+ Key = 'exampleobject_upload' )
50+ uploadid = response ['UploadId' ]
51+ client_for_aes .upload_part (
52+ Bucket = 'examplebucket-1250000000' ,
53+ Key = 'exampleobject_upload' ,
54+ Body = b'bytes' ,
55+ PartNumber = 1 ,
56+ UploadId = uploadid )
57+ response = client_for_aes .list_parts (
58+ Bucket = 'examplebucket-1250000000' ,
59+ Key = 'exampleobject_upload' ,
60+ UploadId = uploadid )
61+ client_for_aes .complete_multipart_upload (
62+ Bucket = 'examplebucket-1250000000' ,
63+ Key = 'exampleobject_upload' ,
64+ UploadId = uploadid ,
65+ MultipartUpload = {'Part' :response ['Part' ]})
66+
67+ # 断点续传方式上传对象,`partsize`大小必须为16字节的整数倍
68+ response = client_for_aes .upload_file (
69+ Bucket = 'test04-123456789' ,
70+ LocalFilePath = 'local.txt' ,
71+ Key = 'exampleobject' ,
72+ PartSize = 10 ,
73+ MAXThread = 10
74+ )
75+
76+ '''使用非对称 RSA 加密每次生成的随机密钥示例
77+ '''
78+
79+ # 方式一:通过密钥值初始化加密客户端
80+ rsa_key_pair = RSAProvider .get_rsa_key_pair ('public_key_value' , 'private_key_value' )
81+
82+ # 方式二:通过密钥路径初始化加密客户端
83+ rsa_key_pair = RSAProvider .get_rsa_key_pair_path ('public_key_path' , 'private_key_path' )
84+
85+ rsa_provider = RSAProvider (key_pair_info = rsa_key_pair )
86+ client_for_rsa = CosEncryptionClient (conf , rsa_provider )
87+
88+ # 上传对象,兼容非加密客户端的 put_object 的所有功能,具体使用可参考 put_object
89+ response = client_for_rsa .put_object (
90+ Bucket = 'examplebucket-1250000000' ,
91+ Body = b'bytes' ,
92+ Key = 'exampleobject' ,
93+ EnableMD5 = False )
94+
95+ # 下载对象,兼容非加密客户端的 get_object 的所有功能,具体使用可参考 get_object
96+ response = client_for_rsa .get_object (
97+ Bucket = 'examplebucket-1250000000' ,
98+ Key = 'exampleobject' )
99+
100+ # 分块上传,兼容非加密客户端的分块上传,除了最后一个 part,每个 part 的大小必须为16字节的整数倍
101+ response = client_for_rsa .create_multipart_upload (
102+ Bucket = 'examplebucket-1250000000' ,
103+ Key = 'exampleobject_upload' )
104+ uploadid = response ['UploadId' ]
105+ client_for_rsa .upload_part (
106+ Bucket = 'examplebucket-1250000000' ,
107+ Key = 'exampleobject_upload' ,
108+ Body = b'bytes' ,
109+ PartNumber = 1 ,
110+ UploadId = uploadid )
111+ response = client_for_rsa .list_parts (
112+ Bucket = 'examplebucket-1250000000' ,
113+ Key = 'exampleobject_upload' ,
114+ UploadId = uploadid )
115+ client_for_rsa .complete_multipart_upload (
116+ Bucket = 'examplebucket-1250000000' ,
117+ Key = 'exampleobject_upload' ,
118+ UploadId = uploadid ,
119+ MultipartUpload = {'Part' :response ['Part' ]})
120+
121+ # 断点续传方式上传对象,`partsize`大小必须为16字节的整数倍
122+ response = client_for_rsa .upload_file (
123+ Bucket = 'test04-123456789' ,
124+ LocalFilePath = 'local.txt' ,
125+ Key = 'exampleobject' ,
126+ PartSize = 10 ,
127+ MAXThread = 10
128+ )
0 commit comments