腾讯云CDN python SDK

博主在开发时偶尔要用到CDN,感觉适合学生党的应该是腾讯云的CDN了,还提供了每月10G的流量,博主平时学习使用已经足够了。

代码

#coding=utf-8
from qcloud_cos import CosClient
from qcloud_cos import UploadFileRequest
from qcloud_cos import StatFileRequest
from qcloud_cos import UpdateFileRequest
from qcloud_cos import MoveFileRequest
from qcloud_cos import DelFileRequest
from qcloud_cos import CreateFolderRequest
from qcloud_cos import UpdateFolderRequest
from qcloud_cos import StatFolderRequest
from qcloud_cos import ListFolderRequest
from qcloud_cos import DelFolderRequest # 设置用户属性, 包括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)

腾讯云CDN python SDK的更多相关文章

  1. Python脚本收集腾讯云CDN日志,并入ELK日志分析

    负责搭建公司日志分析,一直想把CDN日志也放入到日志分析,前些日志终于达成所愿,现在贴出具体做法: 1.收集日志 腾讯云CDN日志一般一小时刷新一次,也就是说当前只能下载一小时之前的日志数据,但据本人 ...

  2. 图片流量节省大杀器:基于腾讯云CDN的sharpP自适应图片技术实践

    目前移动端运营素材大部分依赖图片,基于对图片流量更少,渲染速度更快的诉求,我们推动CDN,X5内核,即通产品部共同推出了一套业务透明,无痛接入的CDN图片优化方案:基于CDN的sharpP自适应图片无 ...

  3. 借助腾讯云CDN开启全站https及问题解决分享

    版权声明:本文由张戈原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/78 来源:腾云阁 https://www.qcloud ...

  4. EasyNVR结合阿里云/腾讯云CDN实现微信/小程序直播的方案

    背景需求: 许多客户有这样的需求:微信公众号做为平台来对摄像机进行直播:可以让用户随时随地打开公共号就可以观看:保证画面的流畅性:保证视频的并发访问量等. 问题分析: 虽然需求看似很简单,其实真正实现 ...

  5. EasyNVR完美搭配腾讯云CDN/阿里云CDN进行RTMP、HLS直播加速的使用说明

    1.相关资料入口 腾讯云LVB EasyNVR.com 2.加速说明 2.1. 腾讯LVB加速 2.1.1. 开通服务 腾讯云视频LVB开通入口 2.1.2. 登录进入控制台 腾讯云直播控制台 2.1 ...

  6. 阿里云 rds python sdk不支持python3处理

    阿里云文档中心的python版本aliyun-python-sdk-rds不支持python3处理 问题:默认情况下文档中心的python版本只支持python2,不兼容python3版本 需要稍微修 ...

  7. 腾讯云--cdn静态内容上传刷新

    一.cdn缓存刷新 当静态内容需要更新时,通常会往COS覆盖上传,不覆盖删除上传等进行更新资源或删除对象存储中的内容. 如果配置的CDN缓存过期时间较长,会导致文件更新后其他边缘节点依旧会缓存旧资源: ...

  8. 金山云 KS3 Python SDK 多线程并发上传文件;下载断点续传 参考脚本

    并发上传 基于py自带模块 concurrent.futures import ThreadPoolExecutor #!/usr/bin/env python3 # -*- coding:utf-8 ...

  9. 腾讯云-搭建 Python 开发环境

    搭建 Python 开发环境 准备工作 任务时间:5min ~ 10min Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.首先我们来看看系统中是否已经存在 Python ,并安装 ...

随机推荐

  1. PAT Basic 1069. 微博转发抽奖(20)

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  2. Java基础知识➣多线程编程(五)

    概述 Java 给多线程编程提供了内置的支持.一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径.使用多线程也是为了充分的利用服务器资源, ...

  3. reconnecting-websocket.js

    websocket是HTML5下一个不错的网络协议解决方案,有一个场景很多猿猿都会遇到,手机锁屏后大约60秒,IOS会自动断开websocket连接,连接丢失了,那我们的数据也就断了.websocke ...

  4. zabbix http服务监控实例

    1在被监控主机安装http服务 ,监听80端口 systemctl start httpd.service       启动服务  80端口已经启动 设定,监控80端口,当服务不当时先自动重启服务 2 ...

  5. VM VirtualBox – Cannot register the hard disk

    第一打开VirtualBox 文件夹,在地址栏输入cmd 第二, 仔细读下面 VBoxManage.exe  internalcommands  sethduuid  "F:\Virtual ...

  6. 查看name的状态,是属于active还是standby

    sudo -E -u hadoop /home/hadoop/bin/hdfs haadmin -getServiceState nn1 sudo -E -u hadoop /home/hadoop/ ...

  7. maven pom.xml(公司版)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  8. 爬虫3 requests之json 把json数据转化为字典

    #json 将json数据转化为字典,方便操作数据 res = requests.get('http://httpbin.org/get') print(res.json()) #res.json() ...

  9. anaconda源配置

    1. 生成配置文件 第一次运行 conda config命令时,将会在用户的home目录创建该文件..condarc配置文件,是一种可选的(optional)运行期配置文件,其默认情况下是不存在的. ...

  10. Linux学习笔记8

    其他常用命令 cd+回车=回车~ 进入当前用户主目录 查看指定进程信息 #ps -ef |grep  进程名 #ps  ---查看属于自己的进程 #ps -aux  查看所有用户的执行进程 换成  p ...