腾讯云分片上传云存储

https://cloud.tencent.com/document/product/436/12270

需要环境:

yum -y install python-requests

用途:

上传备份

使用方法:

# 上传mongo全量备份脚本
[root @ slave bin]# cat /usr/local/bin/cos/backup_cos.py 
#!/usr/bin/env python
#-*- coding: utf- 8  -*-
#Desc:该脚本用于把给定的文件传输到腾讯云的QCOS中
 
import  os,sys
import  logging,time,datetime
from qcloud_cos  import  CosConfig
from qcloud_cos  import  CosS3Client
from qcloud_cos  import  CosServiceError
from qcloud_cos  import  CosClientError
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
# 定义上传云端认证
secret_id =  'access_id'
secret_key =  'access_key'
# 定义上传至哪个区域
region =  'ap-beijing'
# Bucket由bucketname-appid组成
bucket =  'bucket名字'
# 获取配置对象
config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key)
client = CosS3Client(config)
time1=((datetime.datetime.now()).strftime( "%Y%m%d" ))
time_del=((datetime.datetime.now()-datetime.timedelta(days= 15 )).strftime( "%Y%m%d" ))
file_dir =  "/home/backup/full/"  + time1 +  "/tydata/"
files= os.listdir(file_dir)
# 创建分块上传
for  file in files:
   response = client.create_multipart_upload(
     Bucket = bucket,
     Key =  "项目类型/"  + time1 +  "/"  + file
   )
# 获取UploadId供后续接口使用
   uploadid = response[ 'UploadId' ]
# 上传分块
   parts=[]
   pnum= 1
   upload_file = file_dir + file
   with open(upload_file,  'rb' ) as fp:
       while  1 :
           data = fp.read( 2 * 1024 * 1024 * 512 )
           if  not data :
               break ;
           response = client.upload_part(
                   Bucket=bucket,
                   Body=data,
                   Key= "项目类型/"  + time1 +  "/"  + file,
                   PartNumber=pnum,
                   UploadId=uploadid
           )
           etag = response[ 'ETag' ]
           parts.append({ 'ETag'  : etag,  'PartNumber'  : pnum})
           pnum=pnum +  1
# 完成分块上传
   response = client.complete_multipart_upload(
           Bucket=bucket,
           Key= "项目类型/"  + time1 +  "/"  + file,
           UploadId=uploadid,
           MultipartUpload={
                   'Part' : parts
           }
   )

你可能感兴趣的:(系统)