def up_img(self, sourceurl) : u = UpYun(UPCONFIG['bucket'], UPCONFIG['username'], UPCONFIG['password']) #按星期建立目录 _dir = time.strftime('%y%U', time.localtime(time.time())) #获取文件后缀 _m = re.compile('\.\w+$').search(sourceurl) if(_m): filetype = _m.group() else: filetype = '.jpg' #下载到本地文件 localfile = TMPFILE_DIR + str(int(time.time())) + filetype if not u.mkDir(_dir) : return False #img_file = urllib.urlopen(sourceurl, 'rb') if(sourceurl[0:4] == 'http'): try : os.popen('wget "' + sourceurl + '" -q -O ' + localfile) except : return False else: localfile = sourceurl img_file = open(localfile, 'rb') bfile = img_file.read() #源文件md5之后取前16位作为文件名 filename = md5(bfile)[0:16] #上传到upyun文件 upfile = _dir + '/' + filename + filetype if not u.writeFile(upfile, bfile) : os.remove(localfile) return False rgb = self.rbg_img(localfile) _img = Image.open(localfile) size = _img.size if _img.size[0] < 480 or _img.size[1] < 360: return {'toosmall' : True} height = int(size[1] * (640.0/size[0])) ret_data = {'url':upfile,'rgb':rgb, 'height' : height} os.remove(localfile) return ret_data
def test_dd(request): u = UpYun('newavatar', 'one9', 'one9in1234') #查看版本信息 #print u.version() #设定api所调用的域名 #u.setApiDomain('v0.api.upyun.com') #创建目录 #a = u.mkDir('/testa') #print a #a = u.mkDir('/a/b/c', True) 可自动创建父级目录(最多10级) #显示目录下的文件 #a = u.readDir('/test/') #for i in a: # print i.filename #开启调试 #u.debug = True #get user pdb.set_trace() user = User.objects.get(id=301) user_id = user.id pro = user.get_profile() #get user avatar url url_avatar = pro.avatar.url dir_path = url_avatar.split("/") del dir_path[-1] #get upload file path file_path = "/".join(dir_path) #create dir a = u.mkDir('%s' % file_path, True) #get current dir path op = os.path.abspath(os.path.curdir).split() #get current img path userp = url_avatar.split() op.extend(userp) img_path = ''.join(op) #open img data = open('%s' % img_path, 'rb') #设置待上传文件的 Content-MD5 值 #如又拍云服务端收到的文件MD5值与用户设置的不一致,将回报 406 Not Acceptable 错误 u.setContentMD5(md5file(data)) #置待上传文件的 访问密钥(注意:仅支持图片空!,设置密钥后,无法根据原文件URL直接访问,需带 URL 后面加上 (缩略图间隔标志符+密钥) 进行访问) #如缩略图间隔标志符为 ! ,密钥为 bac,上传文件路径为 /folder/test.jpg ,那么该图片的对外访问地址为: http://空间域名/folder/test.jpg!bac #u.setFileSecret('bbbb') #开始上传文件 pdb.set_trace() #get upload img path file_path = file_path.split() img_format = url_avatar.rsplit(".")[-1] user_img_jpg = '%s.%s' % (user_id, img_format) file_path.append(user_img_jpg) upload_path = '/'.join(file_path) #upload img a = u.writeFile('%s' % upload_path, data) print a #a = u.writeFile('/a/b/c/d/e/f/g/logo.jpg',data, True) 可自动创建父级目录(最多10级) #spice upyun current img url domin_url = DOMAIN_NAME.split() domin_url.append(upload_path) upyun_img_url = ''.join(domin_url) # if a: pro.avatar = upyun_img_url pro.save() #获取上传后的图片信息(仅图片空间有返回数据) print(u.getWritedFileInfo('x-upyun-width')) # 图片宽度 print(u.getWritedFileInfo('x-upyun-height')) # 图片高度 print(u.getWritedFileInfo('x-upyun-frames')) # 图片帧数 print(u.getWritedFileInfo('x-upyun-file-type')) # 图片类型 #获取文件信息 print u.getFileInfo('/logo.jpg') #a = u.writeFile('/testd.jpg','sdfsdf') #print a #a = u.deleteFile('/testd.jpg') #print a a = u.readDir('/') if a: for i in a: print i.filename else: print a
m1 = imd5.new() m1.update(src) dest1 = m1.hexdigest() return dest1 sourceurl = 'http://img1.gtimg.com/3/350/35009/3500929_1200x1000_0.jpg' u = UpYun(UPCONFIG['bucket'], UPCONFIG['username'], UPCONFIG['password']) #按星期建立目录 _dir = time.strftime('%y%U', time.localtime(time.time())) #获取文件后缀 _r = re.compile('\.\w+$') filetype = _r.search(sourceurl).group() localfile = '/tmp/weizoo-img-tmp/' + int(time.time()) + filetype if not u.mkDir(_dir) : print '1' #img_file = urllib.urlopen(sourceurl, 'rb') os.popen('wget "' + sourceurl + '" -q -O ' + localfile) img_file = open(localfile, 'rb') bfile = img_file.read() #源文件md5之后取前16位作为文件名 filename = md5(bfile)[0:16] upfile = _dir + '/' + filename + filetype if not u.writeFile(upfile, bfile) : print '2' print upfile
m1 = imd5.new() m1.update(src) dest1 = m1.hexdigest() return dest1 sourceurl = 'http://img1.gtimg.com/3/350/35009/3500929_1200x1000_0.jpg' u = UpYun(UPCONFIG['bucket'], UPCONFIG['username'], UPCONFIG['password']) #按星期建立目录 _dir = time.strftime('%y%U', time.localtime(time.time())) #获取文件后缀 _r = re.compile('\.\w+$') filetype = _r.search(sourceurl).group() localfile = '/tmp/weizoo-img-tmp/' + int(time.time()) + filetype if not u.mkDir(_dir): print '1' #img_file = urllib.urlopen(sourceurl, 'rb') os.popen('wget "' + sourceurl + '" -q -O ' + localfile) img_file = open(localfile, 'rb') bfile = img_file.read() #源文件md5之后取前16位作为文件名 filename = md5(bfile)[0:16] upfile = _dir + '/' + filename + filetype if not u.writeFile(upfile, bfile): print '2' print upfile
} def md5(src): m1 = imd5.new() m1.update(src) dest1 = m1.hexdigest() return dest1 sourceurl = 'http://www.google.com/images/nav_logo102.png' u = UpYun(UPCONFIG['bucket'], UPCONFIG['username'], UPCONFIG['password']) #按星期建立目录 _dir = time.strftime('%y_%U', time.localtime(time.time())) if not (fdir = u.readDir(_dir)) : fdir = u.mkDir(_dir) if not fdir : return False img_file = urllib.urlopen(sourceurl, 'rb') bfile = img_file.read() #获取文件后缀 _r = re.compile('\.\w+$') filetype = _r.search(sourceurl).group() #源文件md5之后取前16位作为文件名 filename = md5(bfile)[0:16] upfile = _dir + '/' + filename + filetype if not u.writeFile(upfile, bfile) : return False
# -*- coding: utf8 -*- from upyun import UpYun,md5,md5file import sys if __name__ == '__main__': u = UpYun('空间名称','操作员用户名','操作员密码') #查看版本信息 print u.version() #设定api所调用的域名 u.setApiDomain('v0.api.upyun.com') #创建目录 a = u.mkDir('/testa') print a a = u.mkDir('/a/b/c', True) 可自动创建父级目录(最多10级) #显示目录下的文件 a = u.readDir('/test/') for i in a: print i.filename #开启调试 u.debug = True data = open('aa.gif','rb') #设置待上传文件的 Content-MD5 值 #如又拍云服务端收到的文件MD5值与用户设置的不一致,将回报 406 Not Acceptable 错误 u.setContentMD5(md5file(data))