COSSDK
##cos sdk 安装
sdkname = cos-python-sdk-v5
sdkname = cos-python-sdk-v5
腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7
pip安装指南:
pip install -U cos-python-sdk-v5
pip install -U cos-python-sdk-v5
cos最新可用地域,参照
python
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
设置用户属性, 包括secret_id, secret_key, region
appid已在配置中移除,请在参数
Bucket中带上appid。Bucket由bucketname-appid组成
python
secret_id = 'AKID15IsskiBQACGbAo6WhgcQbVls7HmuG00' # 替换为用户的secret_id
secret_key = 'csivKvxxrMvSvQpMWHuIz12pThQQlWRW' # 替换为用户的secret_key
region = 'ap-beijing-1' # 替换为用户的region
token = '' # 使用临时秘钥需要传入Token,默认为空,可不填
config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象
client = CosS3Client(config)
secret_id = 'AKID15IsskiBQACGbAo6WhgcQbVls7HmuG00' # 替换为用户的secret_id
secret_key = 'csivKvxxrMvSvQpMWHuIz12pThQQlWRW' # 替换为用户的secret_key
region = 'ap-beijing-1' # 替换为用户的region
token = '' # 使用临时秘钥需要传入Token,默认为空,可不填
config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象
client = CosS3Client(config)
文件流 简单上传
python
file_name = 'test.txt'
with open('test.txt', 'rb') as fp:
response = client.put_object(
Bucket='test04-123456789', # Bucket由bucketname-appid组成
Body=fp,
Key=file_name,
StorageClass='STANDARD',
CacheControl='no-cache',
ContentDisposition='download.txt'
)
print response['ETag']
file_name = 'test.txt'
with open('test.txt', 'rb') as fp:
response = client.put_object(
Bucket='test04-123456789', # Bucket由bucketname-appid组成
Body=fp,
Key=file_name,
StorageClass='STANDARD',
CacheControl='no-cache',
ContentDisposition='download.txt'
)
print response['ETag']
字节流 简单上传
python
response = client.put_object(
Bucket='test04-123456789',
Body='abcdefg',
Key=file_name,
CacheControl='no-cache',
ContentDisposition='download.txt'
)
print response['ETag']
response = client.put_object(
Bucket='test04-123456789',
Body='abcdefg',
Key=file_name,
CacheControl='no-cache',
ContentDisposition='download.txt'
)
print response['ETag']
文件下载 获取文件到本地
python
response = client.get_object(
Bucket='test04-123456789',
Key=file_name,
)
response['Body'].get_stream_to_file('output.txt')
response = client.get_object(
Bucket='test04-123456789',
Key=file_name,
)
response['Body'].get_stream_to_file('output.txt')
文件下载 获取文件流
python
response = client.get_object(
Bucket='test04-123456789',
Key=file_name,
)
fp = response['Body'].get_raw_stream()
print fp.read(2)
response = client.get_object(
Bucket='test04-123456789',
Key=file_name,
)
fp = response['Body'].get_raw_stream()
print fp.read(2)
文件下载 捕获异常
python
try:
response = client.get_object(
Bucket='test04-123456789',
Key='not_exist.txt',
)
fp = response['Body'].get_raw_stream()
print fp.read(2)
except CosServiceError as e:
print e.get_origin_msg()
print e.get_digest_msg()
print e.get_status_code()
print e.get_error_code()
print e.get_error_msg()
print e.get_resource_location()
print e.get_trace_id()
print e.get_request_id()
try:
response = client.get_object(
Bucket='test04-123456789',
Key='not_exist.txt',
)
fp = response['Body'].get_raw_stream()
print fp.read(2)
except CosServiceError as e:
print e.get_origin_msg()
print e.get_digest_msg()
print e.get_status_code()
print e.get_error_code()
print e.get_error_msg()
print e.get_resource_location()
print e.get_trace_id()
print e.get_request_id()