Python脚本06 —— 批量上传图片到七牛服务器

来源:互联网 发布:非正式会谈oo的淘宝店 编辑:程序博客网 时间:2024/05/24 05:00
#coding=utf-8#上传到正式七牛服务器import sys,osfrom qiniu import Authfrom qiniu import BucketManagerimport qiniuaccess_key = ''secret_key = ''bucket_name = ''bucket_domain = ''q = Auth(access_key,secret_key)bucket = BucketManager(q)def upload(f,key):if os.path.splitext(f)[1] in ['.jpg','.png']:mime_type = "image/*"token = q.upload_token(bucket_name, key)ret, info = qiniu.put_file(token, key, f, mime_type=mime_type, check_crc=True)print(info)# def traverse_dir(theDir):# for f in os.listdir(theDir):# afile = os.path.join(theDir,f)# if os.path.isfile(afile):# prefix = os.path.basename(theDir)# if prefix == 'qiniu':# key = f# else:# key = prefix + '/' + f# print key# # upload(afile,key)# elif os.path.isdir(afile):# traverse_dir(afile)# passadir = '/Users/fenggeqiang/Desktop/upload/'# traverse_dir(adir)def scanDir():count = 0for parent,dirnames,filenames in os.walk(adir):print '-----------------------'print "parent is:" + parentprint '-----------------------'count += len(filenames)for filename in filenames:filepath = os.path.join(parent,filename)key = filepath.replace('/Users/fenggeqiang/Desktop/upload/qiniu/','')print "key is:" + keyupload(filepath,key)print countscanDir()

0 0