下载阿里云OSS资源

#!/usr/bin/python

-- coding: utf-8 --

import getopt
import sys
import oss2
import os
import time;

def main(argv):
try:
options, args = getopt.getopt(argv, “hb:t:s✌️”, [“help”, “bucket=”,“source=”, “target=”,“version=”])
except getopt.GetoptError:
sys.exit()
bucketName=""
sourceDir=""
targetDir=""
bakVerion=time.strftime("%Y%m%d%H", time.localtime())
tempPath=""
for option, value in options:
if option in ("-h", “–help”):
print(“help”)
if option in ("-s", “–source”):
sourceDir=value
print(“source is: {0}”.format(sourceDir))
if option in ("-b", “–bucket”):
bucketName=value
print(“bucket is: {0}”.format(bucketName))
if option in ("-t", “–target”):
print(“target is: {0}”.format(value))
targetDir=value
if option in ("-v", “–version”):
print(“version is: {0}”.format(value))
bakVerion=value
print(“bakVerion:”,bakVerion)
print(“error args: {0}”.format(args))
if len(bucketName)==0 or len(sourceDir)==0 or len(targetDir)==0 or len(bakVerion)==0:
print(“parameter is error!”);
return;
auth = oss2.Auth(‘你的akId’, ‘你的ak’)

bucket = oss2.Bucket(auth, ‘OSSURL’, bucketName)
paths=[]
if not sourceDir.endswith("/"):
sourceDir = sourceDir+"/"
if not targetDir.endswith("/"):
targetDir = targetDir+"/"

for obj in oss2.ObjectIterator(bucket, prefix=sourceDir):
if obj.is_prefix():
print(“directory:”+obj.key)
else:
paths.append({“source”:obj.key,“target”:targetDir+bakVerion+"/"+obj.key[len(sourceDir):]})
print(paths)
for ph in paths:
if not ph[“source”].endswith("/"):
dirName=ph[“target”][:ph[“target”].rfind("/")];
if not os.path.exists(dirName):
os.makedirs(dirName);
bucket.get_object_to_file(ph[“source”], ph[“target”])

if name == ‘main’:
main(sys.argv[1:])
print(‘success’)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值