如果文件过大会长期占有上传宽带请谨慎使用

安装腾讯云COSV5Python SDK
pip install -U cos-python-sdk-v5
安装好后复制下面的脚本到deom.py

# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos import CosServiceError
from qcloud_cos import CosClientError

import sys
import logging
import zipfile
import os

startdir = "/www/wwwroot/default"  #要压缩的文件夹路径
file_news = startdir +'.zip' # 压缩后文件夹的名字
z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) #参数一:文件夹名
for dirpath, dirnames, filenames in os.walk(startdir):
    fpath = dirpath.replace(startdir,'') #这一句很重要,不replace的话,就从根目录开始复制
    fpath = fpath and fpath + os.sep or ''#这句话理解我也点郁闷,实现当前文件夹以及包含的所有文件的压缩
    for filename in filenames:
        z.write(os.path.join(dirpath, filename),fpath+filename)
        print ('压缩成功')
z.close()


logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 设置用户属性, 包括secret_id, secret_key, region
secret_id = 'AKIDylkNDVyT8uJFEG9PXfebguxvF'     # 替换为用户的secret_id
secret_key = 'MWZIQqpDwRXGfY8pr9s7y'     # 替换为用户的secret_key
region = 'ap-guangzhou'    # 替换为地区参https://www.qcloud.com/document/product/436/6224的地简称
token = None               # 使用临时秘钥需要传入Token,默认为空,可不填
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
client = CosS3Client(config)

response = client.upload_file(
    Bucket='typecho-125123921', #空间名称自行更改
    LocalFilePath=file_news,
    Key=file_news,
    PartSize=10,
    MAXThread=10
)
print(response['ETag'])

保存好后定时执行python deom.py.

python2.7测试ok

如果觉得我的文章对你有用,请随意赞赏