分享一个python批量上传文件程序,请各位大虾拍砖
命令行模式 python xxx.py -d 云存储路径 -l 本地文件目录
from oss_api import *
from oss_xml_handler import *
from optparse import *
import os,sys
HOST = 'storage.aliyun.com'
ACCESS_ID = 'xxx' #自己填
SECRET_ACCESS_KEY = 'xxx' #自己填
def parseArgs():
parser = OptionParser()
parser.add_option('-d','--dest',action="store",type='string',
dest = 'dest',help='upload dest file names')
parser.add_option('-l','--local',action="store",type='string',
dest = 'local',help='upload local file names')
(options, args) = parser.parse_args()
if not options.dest:
parser.error('option dest must exist')
if not options.local:
parser.error('option local must exist')
return options
def uploadFile(options):
oss = OssAPI(HOST, ACCESS_ID, SECRET_ACCESS_KEY)
for path, dirs, files in os.walk(options.local, 'topdown'):
for name in files:
res = oss.put_object_from_file('xxx', os.path.join(options.dest,name),
os.path.join(path,name),
'application/octet-stream') #自己填bucketname
if (res.status / 100) == 2:
print "File:%s upload succeed" % os.path.join(path,name)
else:
body = res.read()
print "Fail\n%s" % body
if __name__ == '__main__':
options = parseArgs()
uploadFile(options)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。