# -*- coding: utf-8 -*- import oss2 from copy import copy import hashlib,os from optparse import OptionParser parser = OptionParser( usage="%prog -f <upload current dir filename> -m <mode of action(upload or download)> -z <zone(internet or internal)>", version="%prog 1.0") parser.add_option("-f", "--file", action="store", type="string", dest="file", help="upload curren dir file or download oss file") parser.add_option("-m", "--mode", action="store", type="str", dest="mode", help="upload or download") parser.add_option("-z", "--zone", action="store", type="str", dest="zone", help="internet or internal") (options, args) = parser.parse_args() #oss环境参数配置,外部上传采用公网,内部云服务器下载采用内网 #key = 'LTAICtCgStm3g3Hz' key = 'LTAIToEkbVReEy4c' #secret = 'ACTPd7lrSIeB4uoqyhL6nYQNjiCzSJ' secret = 't9bxSJ6x9vfbeJtGu9eGqbGfbCniqW' #host = {"internet" : "oss-cn-qingdao.aliyuncs.com","internal": "oss-cn-qingdao-internal.aliyuncs.com" } #内网与外网连接配置 host = {"internet" : "oss-cn-beijing.aliyuncs.com","internal": "oss-cn-beijing-internal.aliyuncs.com" } #内网与外网连接配置 if options.zone == 'internet': host = host['internet'] elif options.zone == 'internal': host = host['internal'] #bucket = 'zlgxpacket' bucket = 'zlgxpacketnew' '''Md5生产模块''' def GetFileMd5(filename): if not os.path.isfile(filename): return myhash = hashlib.md5() f = file(filename,'rb') while True: b = f.read(8096) if not b: break myhash.update(b) return myhash.hexdigest() '''原型模式''' class initOss(): key = key secret = secret host = host bucket = bucket def sethost(self,host): self.host = host def setbucket(self,bucket): self.bucket = bucket def getservice(self): auth = oss2.Auth(self.key,self.secret) return oss2.Bucket(auth,self.host,self.bucket) #原型模式之浅拷贝 def clone(self): return copy(self) '''桥梁模式''' #两种桥梁的第一种抽象 class Action(): #上传、下载 type = "" filename = "" service = "" def __init__(self,type,filename,service): self.type = type self.filename = filename self.service = service def gettype(self): return self.type def getfilename(self): return self.filename def getservice(self): return self.service class Mode(): #普通、断点续传 action = "" def __init__(self,action): self.action = action # 构造action class Upload(Action): #上传 def __init__(self,filename,service): self.type = 'upload' self.filename = filename self.service = service class Download(Action): #下载 def __init__(self,filename,service): self.type = 'download' self.filename = filename self.service = service #构造mode class Normal(Mode): #普通模式上传与下载 def __init__(self,action): Mode.__init__(self,action) if self.action.gettype() == 'upload': self.action.getservice().put_object_from_file(self.action.getfilename(),self.action.getfilename()) if self.action.gettype() == 'download': self.action.getservice().get_object_to_file(self.action.getfilename(),'zhuyi.txt') class Resumable(Mode): #断点续传 def __init__(self,action): Mode.__init__(self,action) if self.action.gettype() == 'upload': oss2.resumable_upload(self.action.getservice(),self.action.getfilename(),self.action.getfilename()) if self.action.gettype() == 'download': oss2.resumable_download(self.action.getservice(),self.action.getfilename(),self.action.getfilename()) '''装饰器模式''' #定义装饰器 class md5UploadDecorator(): #上传MD5装饰器 def __init__(self,upload): self.upload = upload #生成md5文件 with open('md5_' + self.upload.getfilename(), 'w') as f: f.write(GetFileMd5(self.getfilename())) #上传MD5 文件 Resumable(Upload('md5_' + self.upload.getfilename(),self.upload.getservice())) def gettype(self): return self.upload.gettype() def getservice(self): return self.upload.getservice() def getfilename(self): return self.upload.getfilename() class md5DownloadDecorator(): #下载及比对MD5装饰器 def __init__(self,download): self.download = download #下载文件及MD5 try: Resumable(Download('md5_' + self.download.getfilename(),self.download.getservice())) Resumable(Download(self.download.getfilename(),self.download.getservice())) #生产new MD5 并与old MD5 比对 with open('md5_' + self.download.getfilename(), 'r') as md5file: md5file.seek(0) old_md5 = md5file.readline().split()[0] new_md5 = GetFileMd5(self.download.getfilename()) if old_md5 == new_md5: print u"文件MD5匹配成功" except oss2.exceptions.NotFound: #如果oss上没有md5文件,则只下载文件本身 Resumable(Download(self.download.getfilename(), self.download.getservice())) if __name__=="__main__": initoss = initOss() service = initoss.getservice() if options.mode == 'upload': resumeupload = Resumable(md5UploadDecorator(Upload(options.file, service))) elif options.mode == 'download': resumedownload = md5DownloadDecorator(Download(options.file,service))
ossfile.py 上传文件
最新推荐文章于 2024-11-09 06:00:00 发布