python sdk for tencent qcloud cos v4.0
Project description
介绍
腾讯云COSv4 的Python SDK, 目前可以支持Python2.6与Python2.7。
安装指南
使用pip安装
pip install qcloud_cos_v4
使用easy_install安装
easy_install 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 = "shanghai" # # 替换为用户的region,目前可以为 shanghai/guangzhou
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
Release history Release notifications | RSS feed
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.6.tar.gz
(17.4 kB
view details)
Built Distributions
qcloud_cos_v4-0.0.6-py2.6.egg
(50.9 kB
view details)
File details
Details for the file qcloud_cos_v4-0.0.6.tar.gz
.
File metadata
- Download URL: qcloud_cos_v4-0.0.6.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ece35f33f722186169d055cddc9c0bee8e43bdb58411e155bb10768a4203e2ba |
|
MD5 | 872fd9203385c17d0be72ee28f3ac3f0 |
|
BLAKE2b-256 | 11c29b9d29b1d5f75b4335a37c80140de67c6638901814ede01a7cda7750266a |
File details
Details for the file qcloud_cos_v4-0.0.6.linux-x86_64.tar.gz
.
File metadata
- Download URL: qcloud_cos_v4-0.0.6.linux-x86_64.tar.gz
- Upload date:
- Size: 40.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59fc6dc5282b55aa9b1274ffb2c0980a8b9738ff5e2c5f6f23c8a552ef651ed9 |
|
MD5 | 3ff3dab8175be1f2ba193f2e8a23b27e |
|
BLAKE2b-256 | 15c27e26f0f323f1589a13d281222fe1b7549b1615c5937a0caf9104b05163b4 |
File details
Details for the file qcloud_cos_v4-0.0.6-py2.6.egg
.
File metadata
- Download URL: qcloud_cos_v4-0.0.6-py2.6.egg
- Upload date:
- Size: 50.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14a5e7e1518656648ec4a0e96f56b3b43e6e93e5e79398de65193037ef409e88 |
|
MD5 | 55ea1e8a77084a94f4f9752860e5c8b0 |
|
BLAKE2b-256 | c57e181129bf340a70a967238ee3cddb1d83ab9db90dba73377d5ba7643d5b06 |
File details
Details for the file qcloud_cos_v4-0.0.6-py2-none-any.whl
.
File metadata
- Download URL: qcloud_cos_v4-0.0.6-py2-none-any.whl
- Upload date:
- Size: 24.2 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38e615a20721055ba91397c9a7942e0e3281e06a2145f97f9e29c6b717595f42 |
|
MD5 | d8748f05401df09e4d55145819cc421b |
|
BLAKE2b-256 | 6a0196c2c60d73c98e3e94c3aaed72f2a7bef78f318e403092379590d7545869 |