上传阿里云oss

这里写自定义目录标题

#!/usr/bin/python

-- coding: utf-8 --

import getopt
import sys
import oss2
import os

def getFiles(filepath,paths):
files = os.listdir(filepath)
for fi in files:
fi_d = os.path.join(filepath,fi)
if os.path.isdir(fi_d):
getFiles(fi_d,paths)
else:
paths.append(os.path.join(filepath,fi_d.replace(’\’,’/’)))

def main(argv):
try:
options, args = getopt.getopt(argv, “hb:t:s:”, [“help”, “bucket=”, “source=”, “target=”])
except getopt.GetoptError:
sys.exit()
bucketName=""
targetDir=""
sourceDir=""
tempPath=""
for option, value in options:
if option in ("-h", “–help”):
print(“help”)
if option in ("-b", “–bucket”):
bucketName=value
print(“bucket is: {0}”.format(bucketName))
if option in ("-s", “–source”):
sourceDir=value
print(“source is: {0}”.format(sourceDir))
if option in ("-t", “–target”):
print(“target is: {0}”.format(value))
targetDir=value
print(“error args: {0}”.format(args))
if len(bucketName)==0 or len(sourceDir)==0 or len(targetDir)==0:
print(“parameter is error!”);
return;

auth = oss2.Auth(‘你的akId’, ‘你的ak’)

bucket = oss2.Bucket(auth, ‘OSSURL’, bucketName)
paths=[]
getFiles(sourceDir,paths);
print(paths)
if not sourceDir.endswith("/"):
sourceDir = sourceDir+"/"
if not targetDir.endswith("/"):
targetDir = targetDir+"/"
for item in paths:
tempPath=item[len(sourceDir):];
print(targetDir+tempPath)
bucket.put_object_from_file(targetDir+tempPath, item)

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

发布了8 篇原创文章 · 获赞 0 · 访问量 12

猜你喜欢

转载自blog.csdn.net/weixin_41813300/article/details/105063658