Skip to main content

python sdk for tencent qcloud cos v4.0

Project description

https://travis-ci.org/tencentyun/cos-python-sdk-v4.svg?branch=master https://badge.fury.io/py/qcloud_cos_v4.png Documentation Status Hex.pm

介绍

腾讯云COSv4 的Python SDK, 目前可以支持Python2.6与Python2.7。

安装指南

使用pip安装

pip install -U qcloud_cos_v4

手动安装:

python setup.py install

使用方法

使用python sdk,参照sample.py

 # 设置用户属性, 包括appid, secret_id和secret_key
 # 这些属性可以在cos控制台获取(https://console.qcloud.com/cos)
 appid = 100000                  # 替换为用户的appid
 secret_id = u'xxxxxxxx'         # 替换为用户的secret_id
 secret_key = u'xxxxxxx'         # 替换为用户的secret_key
region_info = "sh" #           # 替换为用户的region,目前可以为 sh/gz/tj/sgp,分别对应于上海,广州,天津,新加坡园区
 cos_client = CosClient(appid, secret_id, secret_key, region=region_info)

 # 设置要操作的bucket
 bucket = u'mybucket'

 ############################################################################
 # 文件操作                                                                 #
 ############################################################################
 # 1. 上传文件(默认不覆盖)
 #    将本地的local_file_1.txt上传到bucket的根分区下,并命名为sample_file.txt
 #    默认不覆盖, 如果cos上文件存在,则会返回错误
 request = UploadFileRequest(bucket, u'/sample_file.txt', u'local_file_1.txt')
 upload_file_ret = cos_client.upload_file(request)
 print 'upload file ret:', repr(upload_file_ret)

 # 2. 上传文件(覆盖文件)
 #    将本地的local_file_2.txt上传到bucket的根分区下,覆盖已上传的sample_file.txt
 request = UploadFileRequest(bucket, u'/sample_file.txt', u'local_file_2.txt')
 request.set_insert_only(0)  # 设置允许覆盖
 upload_file_ret = cos_client.upload_file(request)
 print 'overwrite file ret:', repr(upload_file_ret)

 # 3. 获取文件属性
 request = StatFileRequest(bucket, u'/sample_file.txt')
 stat_file_ret = cos_client.stat_file(request)
 print 'stat file ret:', repr(stat_file_ret)

 # 4. 更新文件属性
 request = UpdateFileRequest(bucket, u'/sample_file.txt')

 request.set_biz_attr(u'这是个demo文件')           # 设置文件biz_attr属性
 request.set_authority(u'eWRPrivate')              # 设置文件的权限
 request.set_cache_control(u'cache_xxx')           # 设置Cache-Control
 request.set_content_type(u'application/text')     # 设置Content-Type
 request.set_content_disposition(u'ccccxxx.txt')   # 设置Content-Disposition
 request.set_content_language(u'english')          # 设置Content-Language
 request.set_x_cos_meta(u'x-cos-meta-xxx', u'xxx') # 设置自定义的x-cos-meta-属性
 request.set_x_cos_meta(u'x-cos-meta-yyy', u'yyy') # 设置自定义的x-cos-meta-属性

 update_file_ret = cos_client.update_file(request)
 print 'update file ret:', repr(update_file_ret)

 # 5. 更新后再次获取文件属性
 request = StatFileRequest(bucket, u'/sample_file.txt')
 stat_file_ret = cos_client.stat_file(request)
 print 'stat file ret:', repr(stat_file_ret)

 # 6. 移动文件, 将sample_file.txt移动位sample_file_move.txt
 request = MoveFileRequest(bucket, u'/sample_file.txt', u'/sample_file_move.txt')
 stat_file_ret = cos_client.move_file(request)
 print 'move file ret:', repr(stat_file_ret)

 # 7. 删除文件
 request = DelFileRequest(bucket, u'/sample_file_move.txt')
 del_ret = cos_client.del_file(request)
 print 'del file ret:', repr(del_ret)

 ############################################################################
 # 目录操作                                                                 #
 ############################################################################
 # 1. 生成目录, 目录名为sample_folder
 request = CreateFolderRequest(bucket, u'/sample_folder/')
 create_folder_ret = cos_client.create_folder(request)
 print 'create folder ret:', create_folder_ret

 # 2. 更新目录的biz_attr属性
 request = UpdateFolderRequest(bucket, u'/sample_folder/', u'这是一个测试目录')
 update_folder_ret = cos_client.update_folder(request)
 print 'update folder ret:', repr(update_folder_ret)

 # 3. 获取目录属性
 request = StatFolderRequest(bucket, u'/sample_folder/')
 stat_folder_ret = cos_client.stat_folder(request)
 print 'stat folder ret:', repr(stat_folder_ret)

 # 4. list目录, 获取目录下的成员
 request = ListFolderRequest(bucket, u'/sample_folder/')
 list_folder_ret = cos_client.list_folder(request)
 print 'list folder ret:', repr(list_folder_ret)

 # 5. 删除目录
 request = DelFolderRequest(bucket, u'/sample_folder/')
 delete_folder_ret = cos_client.del_folder(request)
 print 'delete folder ret:', repr(delete_folder_ret)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

qcloud_cos_v4-0.0.9.tar.gz (17.5 kB view details)

Uploaded Source

qcloud_cos_v4-0.0.9.linux-x86_64.tar.gz (40.6 kB view details)

Uploaded Source

Built Distributions

qcloud_cos_v4-0.0.9-py2.6.egg (51.0 kB view details)

Uploaded Source

qcloud_cos_v4-0.0.9-py2-none-any.whl (24.4 kB view details)

Uploaded Python 2

File details

Details for the file qcloud_cos_v4-0.0.9.tar.gz.

File metadata

File hashes

Hashes for qcloud_cos_v4-0.0.9.tar.gz
Algorithm Hash digest
SHA256 28988978041f7297f31b84f58479f22515cc2d81bf4b0b12180e31f73b003e04
MD5 4fe86d0a5f94a9b5a3381ae52508ea7e
BLAKE2b-256 caa4dd23eb7817076ccd5f01546e3ee4c87b333d5f4ab739486e284e59dabcf1

See more details on using hashes here.

File details

Details for the file qcloud_cos_v4-0.0.9.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for qcloud_cos_v4-0.0.9.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 601e2dd274fc9ec87cfe4fdf8841c18ec04a9015617526325eca5eb0f15a3300
MD5 b71afc53aa88b28925954c96b3f3a803
BLAKE2b-256 66b2ad04c9172edab6ab913aff5c1a34438d7a3649937ecc4fc11f5220ed8e9a

See more details on using hashes here.

File details

Details for the file qcloud_cos_v4-0.0.9-py2.6.egg.

File metadata

File hashes

Hashes for qcloud_cos_v4-0.0.9-py2.6.egg
Algorithm Hash digest
SHA256 26bba5b04c3de2f4c0b1a7eedb1619aee48b460ae58f362b489e567b885002e4
MD5 2e157aa253884143de44a35b24a3ad1e
BLAKE2b-256 36df236b74acf68ee37cc73f2c6ed5f102cb84a4d802aa8a74ba4471d2d7f301

See more details on using hashes here.

File details

Details for the file qcloud_cos_v4-0.0.9-py2-none-any.whl.

File metadata

File hashes

Hashes for qcloud_cos_v4-0.0.9-py2-none-any.whl
Algorithm Hash digest
SHA256 3b6e47f7cd905b9fb3a0de669d758b0c5e3817aa12e94f86377961b216fdcb9a
MD5 d4f3b194ce927956e638e5a9797f0778
BLAKE2b-256 4e178e2b6bd358875be35581256db42d92c0d40bd6ab3af97bec66c6692ae8ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page