阿里云分片上传云存储

#!/usr/bin/env python
#-*- coding: utf- 8  -*-
#Filename: backupToOSS.py
#Desc:该脚本用于把指定的文件传输到阿里云的OSS中。
 
import  datetime
import  oss2
import  logging
import  sys
#阿里云access_key、secret_key、bucket、endpoint
access_key = xxxxxxxxxxxxx
secret_key = xxxxxxxxxxxxx
bucket_name = xxxxxxxx
endpoint = xxx.aliyuncs.com
# 上传文件到bucket 的dirname目录
dirname = xx
# 设置时间间隔
days_ago = xx
# 需要备份的文件位置
filepath = /xxx/xxxx/xxx/
 
for  param in (access_key, secret_key, bucket, endpoint):
     assert  '<'  not in param,  '请设置参数:'  + param
 
bucket = oss2.Bucket(oss2.Auth(access_key, secret_key), endpoint, bucket_name +  '/'  + dirname )
# 获取当天时间以前 days_ago天 的日期,精确到天
time1=((datetime.datetime.now()-datetime.timedelta(days=days_ago)).strftime( "%Y%m%d" ))
# 需要上传的文件:filepath下 以 年月日.tgz 命名的文件
file_upload = filepath + time1 +  ".tgz"
ossName = time1 +  ".tgz"
nowTime = datetime.datetime.now().strftime( '%Y%m%d_%T' )
 
try :
     bucket.put_object_from_file(ossName, file_upload)
except IOError:
     logging.exception( "######" +nowTime+ "######" )

猜你喜欢

转载自blog.csdn.net/sinat_25545645/article/details/81018154
今日推荐