def upload(self, upload_dir, upload_bucket): if os.path.isdir(upload_dir): if os.path.isdir(upload_dir): up_file_list = [] for root, dirs, files in os.walk(upload_dir,topdown=True): for v_file in files: up_file_list.append(os.path.join(root, v_file)) for file_name in up_file_list: token = self.mauth.upload_token(upload_bucket, file_name) ret, info = put_file(token, file_name, file_name) # print(info) assert ret['key'] == file_name assert ret['hash'] == etag(file_name) print ret elif os.path.isfile(upload_dir): token = self.mauth.upload_token(upload_bucket, file_name) ret, info = put_file(token, file_name, file_name) assert ret['key'] == file_name assert ret['hash'] == etag(file_name) print ret elif os.path.isfile(upload_dir): file_name = upload_dir token = self.mauth.upload_token(upload_bucket, file_name) ret, info = put_file(token, file_name, file_name) assert ret['key'] == file_name assert ret['hash'] == etag(file_name) print ret
def img_upload(): if request.method == 'POST': ff = request.files['file'] filesname = "".join(lazy_pinyin(ff.filename)) d = os.path.dirname(__file__) basepath = os.path.dirname(d) upload_path = os.path.join(basepath, '../dist/imges', secure_filename(filesname)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 upload_path_min = os.path.join(basepath, '../dist/imges', secure_filename('min'+filesname)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径 ff.save(upload_path) ResizeImage(upload_path, upload_path_min) times = time.strftime("%Y%m%d-%H%M%S", time.localtime()) uid = str(random.randint(1000, 9999)) # str用于把数字转换成字符串 uid = times + uid m = secure_filename(filesname) sname = uid + m sname_min = 'min' + uid + m # 需要填写你的 Access Key 和 Secret Key access_key = 'SvHVcX7ZotlGtp7Lpxy1Az95Ae_tC8lOqnC02A9C' secret_key = 'blSArZE6UCrYABtjaYwTFLHeBXCeofUKjpoEKspi' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'fa' # 上传后保存的文件名 key = sname token = q.upload_token(bucket_name, key, 3600) localfile = upload_path ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) url_st = 'http://img.lenyuan.com/' + sname # 上传压缩图片 key = sname_min token = q.upload_token(bucket_name, key, 3600) localfile = upload_path_min ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) url_st_min = 'http://img.lenyuan.com/' + sname_min os.remove(upload_path) os.remove(upload_path_min) data = { 'status': 200, 'message': 'ok', 'data': { 'url': url_st, 'url_min': url_st_min }, } data = json.dumps(data, default=lambda o: o.__dict__) return data, 200, {"ContentType": "application/json"}
def upload(self, upload_dir, upload_bucket): if os.path.isdir(upload_dir): if os.path.isdir(upload_dir): up_file_list = [] for root, dirs, files in os.walk(upload_dir, topdown=True): for v_file in files: up_file_list.append(os.path.join(root, v_file)) for file_name in up_file_list: token = self.mauth.upload_token(upload_bucket, file_name) ret, info = put_file(token, file_name, file_name) # print(info) assert ret['key'] == file_name assert ret['hash'] == etag(file_name) print ret elif os.path.isfile(upload_dir): token = self.mauth.upload_token(upload_bucket, file_name) ret, info = put_file(token, file_name, file_name) assert ret['key'] == file_name assert ret['hash'] == etag(file_name) print ret elif os.path.isfile(upload_dir): file_name = upload_dir token = self.mauth.upload_token(upload_bucket, file_name) ret, info = put_file(token, file_name, file_name) assert ret['key'] == file_name assert ret['hash'] == etag(file_name) print ret
def avatar_img_url(): img = request.form['img'] access_key = 'SvHVcX7ZotlGtp7Lpxy1Az95Ae_tC8lOqnC02A9C' secret_key = 'blSArZE6UCrYABtjaYwTFLHeBXCeofUKjpoEKspi' times = time.strftime("%Y%m%d-%H%M%S", time.localtime()) uid = str(random.randint(1000, 9999)) # str用于把数字转换成字符串 uid = times + uid strs = img.split(',')[-1] sname ='/var/www/html/flask/dist/imges/avatar_img_'+uid+'.jpeg' sname_name ='avatar_img_'+uid+'.jpg' sname_min ='/var/www/html/flask/dist/imges/avatar_img_min_'+uid+'.jpeg' sname_min_name ='avatar_img_min_'+uid+'.jpeg' with open(sname, 'wb') as f: f.write(base64.b64decode(strs)) ResizeImage(sname, sname_min) # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'fa' # 上传后保存的文件名 key = sname_name token = q.upload_token(bucket_name, key, 5000) localfile = sname ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) url_st = 'http://img.lenyuan.com/' + key # 上传压缩图片 key = sname_min_name token = q.upload_token(bucket_name, key, 5000) localfile = sname_min ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) url_st_min = 'http://img.lenyuan.com/' + key os.remove(sname) os.remove(sname_min) data = { 'status': 200, 'message': 'ok', 'data': { 'url': url_st, 'url_min': url_st_min }, } data = json.dumps(data, default=lambda o: o.__dict__) return data, 200, {"ContentType": "application/json"}
def user_icon(request): if request.method == 'GET': return render(request, 'home/user/icon.html') if request.method == 'POST': file = request.FILES['avatar'] with open(BASE_DIR+'/static/images/icon.jpg', 'wb') as pic: for c in file.chunks(): pic.write(c) user = get_user(request) # 要上传的空间 bucket_name = 'goodbuy' # 构建鉴权对象 q = qiniu.Auth(ACCESS_KEY, QINIU_SECRET_KEY) # 上传到七牛后保存的文件名 key = 'static/icon/'+user.username+'3.jpg' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key) localfile = BASE_DIR+'/static/images/icon.jpg' ret, info = qiniu.put_file(token, key=key, file_path=localfile) print(info, ret) assert ret['key'] == key assert ret['hash'] == etag(localfile) user.icon = 'http://pbu0s2z3p.bkt.clouddn.com/'+key user.save() return JsonResponse({'key':key,'token':token})
def upload(file): # 指定要上传至的七牛云的空间 filename = file['name'] print(filename) # 上传到七牛后保存的文件名 keyname = str(uuid.uuid4()) + '.' + filename.split('.')[1] print(keyname) # 生成上传 Token,可以指定过期时间等 token = q.upload_token(QINIU_BUCKET_NAME, keyname, 3600) print(token) # # 保存文件到本地服务器,如果不想缓存到本地可以不写 file = file['img'] # /static/xx.png 路径名 localfilepath = LOCALDIR + keyname with open(localfilepath, 'wb') as fp: for c in file.chunks(): fp.write(c) # 要上传到七牛云的图片名 ret, info = put_file(token, keyname, localfilepath) print('七牛云存储') print(info) print(ret) assert ret['key'] == keyname assert ret['hash'] == qiniu.etag(localfilepath) # # 此处要写加入数据库代码 src = CLOUDHOST + keyname # 七牛云的图片地址,传给前台并写入数据库 print(src) return {"srcaddr": src}
def upload_video(bucket, local): bucket_mp4_video = 'video-mp4-test' bucket_video_cover_image = 'vframe-test' pipeline = 'video-transcode' q = Auth(access_key, secret_key) # 构造视频转码参数 fops_transcode = 'avthumb/mp4/vb/1.25m' save_key = '$(endUser)/$(year)/$(mon)/$(day)/$(etag).mp4' saveas_key = urlsafe_base64_encode('{}:{}'.format(bucket_mp4_video, save_key)) fops_transcode = '{}|saveas/{}'.format(fops_transcode, saveas_key) # 构造截图参数 fops_vframe = 'vframe/jpg/offset/1' save_key = '$(endUser)/$(year)/$(mon)/$(day)/$(etag).jpg' saveas_key = urlsafe_base64_encode('{}:{}'.format(bucket_video_cover_image, save_key)) fops_vframe = '{}|saveas/{}'.format(fops_vframe, saveas_key) persistentOps = ';'.join([fops_transcode, fops_vframe]) print('persistentOps: {}'.format(persistentOps)) policy = { 'scope': '{}:$(endUser)'.format(bucket), 'saveKey': '$(endUser)/$(year)/$(mon)/$(day)/$(etag)$(ext)', 'endUser': '******', 'persistentOps': persistentOps, 'persistentPipeline': pipeline } token = q.upload_token(bucket, policy=policy) ret, info = put_file(token, None, local) print(ret) print(info) assert ret['key'] == etag(local)
def push_qiniu(): image_name = get_url()[0] # 七牛云的accesskey以及secretkey access_key = "xxx" secret_key = "xxx" # 上传至七牛云的文件名 time = datetime.datetime.now() dir = 'bing_images/{}-{}-{}/'.format(time.year, time.month, time.day) key = dir + image_name # 验证七牛云身份 q = Auth(access_key, secret_key) # 七牛云的存储名字 bucket_name = "lwg-cunchu" # 上传 token = q.upload_token(bucket_name, key, 3600) local_image = "./images/{}-{}-{}/{}".format(time.year, time.month, time.day, image_name) ret, info = put_file(token, key, local_image) print(info) assert ret['key'] == key assert ret['hash'] == etag(local_image)
def sync(conf, demon): try: data = load_config(conf) except IOError: raise for k, v in data.items(): data[k] = v.encode('utf-8') qc = qiniuClient(data['access_key'], data['secret_key'], data['bucket_name'], data['domain'], data['root']) for item in qc.list(): path = os.path.join(data['root'], item['key']) if (not os.path.exists(path) or os.path.exists(path) and qiniu.etag(path) != item['hash'] ): if qc.down_file(item['key'], path): msg = "下载{}成功".format(path) else: msg = "下载{}失败".format(path) bubble_notify = pynotify.Notification("qiniuClund", msg ) bubble_notify.show() if demon: wm = pyinotify.WatchManager() event_flags = (pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY) wm.add_watch(data['root'], event_flags, rec=True, auto_add=True) eh = qiniudEventHandler(qc) notifier = pyinotify.Notifier(wm, eh) notifier.loop()
def upload_to_QINIU(self): from qiniu import Auth, put_file, etag import qiniu.config import time from my_info import QINIU_KEY, QINIU_SECRET, QINIU_BUCKET, QINIU_DEFAULT_DOMAIN try: access_key = QINIU_KEY secret_key = QINIU_SECRET #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = QINIU_BUCKET #上传到七牛后保存的文件名 year, month, day = time.localtime()[0:3] key = '{}_{}_{}_{}'.format(year, month, day, self.fileName) #生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) #要上传文件的本地路径 localfile = self.filePath ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) pic_link = QINIU_DEFAULT_DOMAIN + key reply_data = self.rend_upload_return(True, pic_link, 'qiniu') except Exception as e: reply_data = self.rend_upload_return(False, e, 'qiniu') finally: return reply_data
def qn_upload(object): remote = get_md5(object) + "/" + os.path.split(object)[1] token = qn.upload_token("upload", remote) ret = qiniu.put_file(token, remote, object)[0] # check assert ret["hash"] == qiniu.etag(object) print("http://cloud.jacksao.wang/%s" % remote)
def upload_file(self, key, localfile): token = self._auth.upload_token(self._bucket, key) ret, info = put_file(token, key, localfile) if ret: # ret possibly is None assert ret['key'] == key assert ret['hash'] == etag(localfile) return info
def get_url_token(file_path, file_name): from qiniu import Auth, put_file, etag import qiniu.config # 需要填写你的 Access Key 和 Secret Key access_key = settings.access_key secret_key = settings.secret_key # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'pics' # 上传到七牛后保存的文件名 key = file_name # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600 * 24 * 3600) # 要上传文件的本地路径 localfile = file_path ret, info = put_file(token, key, localfile) url = 'http://pdwd5ogz2.bkt.clouddn.com/' + key # print(url) try: assert ret['key'] == key assert ret['hash'] == etag(localfile) except: return None return url
def upload2qiniu(key=None, localfile=None): # 生成上传 Token,可以指定过期时间等 try: if key == None: print('请输入保存的文件名') return False if localfile == None: print('请填写要上传的文档名') return False today = date.today().strftime('%Y-%m-%d') #token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 key = key + today + '.json' print(key) localfile = './' + localfile + today + '.json' print(localfile) token = q.upload_token(bucket_name, key, 3600) ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile) print('成功上传文档{}至{}'.format(localfile, bucket_name)) return True except Exception as e: print(e.args)
def cronTask(): logger.info('enter task') dbFile = 'trades_' + datetime.now().strftime("%Y-%m-%d") + '.db' conn = sqlite3.connect(dbFile) cursor = conn.cursor() cursor.execute(CREATE_SQL) OKEXTask(cursor) bittrexTask(cursor) bitfinexTask(cursor) poloniex(cursor) conn.close() regex = re.compile('trades_.*.db') files = sorted(filter(regex.match, os.listdir('.')), reverse=True) if len(files) > 1 and os.environ.has_key('access_key'): access_key = os.environ['access_key'] secret_key = os.environ['secret_key'] q = Auth(access_key, secret_key) bucket_name = 'stock' for item in files[1:]: token = q.upload_token(bucket_name, item, 60) ret, info = put_file(token, item, item) print info if ret['hash'] == etag(item): os.remove(item) logger.info('leave task')
def upload_file_to_qiniu(file_path, file_name): print("[%s] uploading......" % file_name) token = q.upload_token(config['bucket_name'], file_name) ret, _ = put_file(token, file_name, file_path) assert ret['key'] == file_name assert ret['hash'] == etag(file_path) return os.path.join(config['bucket_url'], ret['key'])
def upload_file(localfile): # 需要填写你的 Access Key 和 Secret Key access_key = 'aRzVj18_VFA_p4EZ9z8ClWkVwYvAOWuoMStYnJhi' secret_key = 'bh_hBotfph77wcmHHIgCwExqKEvQN_eyT5m1r9_c' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'myselfres' # 上传到七牛后保存的文件名 key = time.strftime("%Y-%m-%d-%H-%M-%S.png", time.localtime()) print key # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 ret, info = put_file(token, key, localfile) print (info) assert ret['key'] == key assert ret['hash'] == etag(localfile) pngOnlinePath = myselfres_dom + key + ')' setText(pngOnlinePath)
def upload_to_qiniu(localfile,key): # 将本地文件上传到七牛云 ''' Args: localfile:本地文件位置 key:上传到云服务器后的文件名 ''' #生成上传 Token,可以指定过期时间等 token = QN_AUTH.upload_token(config.QN_BUCKET, key, 3600) #要上传文件的本地路径 ret, info = put_file(token, key, localfile) # assert 断点验证,如果成功则通过不成功则报异常 assert ret['key'] == key assert ret['hash'] == etag(localfile) url = get_qn_url(key) return ret,info,url print(info)
def getImg1(html, uuid, categoryId): path = 'E:\\Work\\program\\toutiao\\' if not os.path.isdir(path): os.mkdir(path) soup = BeautifulSoup(html, 'html.parser') images = soup.find_all('img') urls = [] for img in images: link = 'http://' + img.get('src')[2:] #name = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') name = datetime.datetime.now().strftime( '%Y%m%d%H%M%S%f') + '_' + uuid + '_' + categoryId urllib.request.urlretrieve(link, '{}{}.jpg'.format(path, name)) fileName = name + '.jpg' localFale = './' + fileName token = q.upload_token(bucket_name, fileName, 3600) ret, info = put_file(token, fileName, localFale) assert ret['key'] == fileName assert ret['hash'] == etag(localFale) url = 'http://pnvqejtth.bkt.clouddn.com/' newUrl = url + fileName + '?imageView2/2/h/70' urls.append(newUrl) # img['src'] = newUrl print(newUrl) return urls
def down_load(): # flake8: noqa from qiniu import Auth, put_file, etag import qiniu.config # 需要填写你的 Access Key 和 Secret Key access_key = "LfXjXvG1e6vPXV2UFLfXrJ14uNM792vqONhnLyB2" secret_key = "MRL5Y67LWpl_9_pOUPa7OvSY-t8OD9R2pR3luUA_" # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'flask-ihome-python65' # 上传后保存的文件名 可以指明, 也可以不指明(计算结果值当作文件名) key = 'my-python-logo.png' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 localfile = r'H:\我的文档\me_information\无花果.jpg' ret, info = put_file(token, key, localfile) print("info:", info) print(" ") assert ret['key'] == key assert ret['hash'] == etag(localfile) '''
def upload(localfile, bucket_name, key): token = q.upload_token(bucket_name, key, 3600) ret, info = put_file(token, key, localfile) if DEBUG: print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
def upload(Compress=Config.compress, refresh_flag=True): if not Compress: file = Config.updateData_FILE else: file = Config.updateData_FILE_Compress if datetime.today().weekday() in [0, 1, 2, 3, 4]: key = Config.edbus_weekday else: key = Config.edbus_weekend try: localfile = Config.updateData_DIR + file token = Config.q.upload_token(Config.BUCKET_NAME, key, 3600) ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile) print('成功上传文档{}至{}'.format(key, Config.BUCKET_NAME)) if refresh_flag: sleep(5) result = refresh() # return '成功上传文档{}至{}'.format(key, Config.BUCKET_NAME) return info, result except Exception as e: print(e.args) raise e
def upload_pic(filename): # parser = OptionParser() # parser.add_option("", "--filename", dest="filename", help="filename") # (opts, args) = parser.parse_args() #需要填写你的 Access Key 和 Secret Key access_key = 'key' secret_key = 'secret' #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = 'bucket' #上传到七牛后保存的文件名 arr = filename.split("/") key = arr[len(arr) - 1] #生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) #要上传文件的本地路径 localfile = filename ret, info = put_file(token, key, localfile) bucket = BucketManager(q) print("http://image.blueskykong.com/" + key) assert ret['key'] == key assert ret['hash'] == etag(localfile) return info
def save_mp3(): access_key = '0ZT-Rd0AswhPQti5lX2Ytt1T6XkyM80eY_4w9Pm9' secret_key = 'MbscrgLx_FefkUZ21SjY-GRE1oPJcvP2vvN6oXgW' # 初始化Auth状态 q = Auth(access_key, secret_key) # 你要测试的空间, 并且这个key在你空间中存在 bucket_name = 'live-bucket' key = 'mayun12_13.mp4' # 指定转码使用的队列名称 pipeline = 'image-pipeline' # 设置转码参数(以视频转码为例) fops = 'avthumb/mp3/ar/48000/vn/1|saveas/' # 通过添加'|saveas'参数,指定处理后的文件保存的bucket和key,不指定默认保存在当前空间,bucket_saved为目标bucket,bucket_saved为目标key saveas_key = urlsafe_base64_encode(bucket_name + ':mayun_mp3_test.mp3') fops = fops + saveas_key # 在上传策略中指定fobs和pipeline policy = {'persistentOps': fops, 'persistentPipeline': pipeline} token = q.upload_token(bucket_name, key, 3600, policy) localfile = '/Users/ryanxu/Downloads/mayun.mp4' ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
def qiniu_load(file,path): access_key = 'y8BaldA683hgVEhHix4_xWR3NESm9uch28e1nG30' secret_key = '7Rqb5UoDbg7B3BVEdG38ZtHUzkQzTh6fU_TFOn61' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'yljewlery' # 上传到七牛后保存的文件名 key = file # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key) # 要上传文件的本地路径 localfile = path ret, info = put_file(token, key, localfile) print(ret) print(info) if is_py2: assert ret['key'].encode('utf-8') == key elif is_py3: assert ret['key'] == key assert ret['hash'] == etag(localfile)
def upload(self, local_file_path, file_name): return_save_file_path = None try: # 需要填写你的 Access Key 和 Secret Key access_key = settings.QINIU_ACCESS_KEY secret_key = settings.QINIU_SECRET_KEY # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = settings.QINIU_BUCKET_NAME # 上传到七牛后保存的文件名 key = file_name # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 localfile = local_file_path ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile) return_save_file_path = file_name return return_save_file_path except Exception, e: print e return return_save_file_path
def upload(origin_file_path): # 构建鉴权对象 q = Auth(config.access_key, config.secret_key) # 要上传的空间 bucket_name = 'md-doc' localfile = conwebp.convert(origin_file_path) # 上传到七牛后保存的文件名 dest_prefix = time.strftime("%Y%m%d%H%M%S", time.localtime()) dest_name = dest_prefix + "_" + os.path.basename(localfile) # 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器。 policy = { 'callbackBody': 'filename=$(fname)&filesize=$(fsize)' } token = q.upload_token(bucket_name, dest_name, 3600, policy) ret, info = put_file(token, dest_name, localfile) if ret is not None: print("Upload Success,url=", config.domin + dest_name) else: print("info=", info) print("ret=", ret) assert ret['key'] == dest_name assert ret['hash'] == etag(localfile)
def upload(args): """ Upload images to qiniu cloud """ assert (args.bucket_name != None) assert (args.remote_dir != None) assert (args.input_file != None) assert (args.local_dir != None) bucket_name = args.bucket_name # with open(args.input_file) as fh: # paths = filter(None, fh.readlines()) img_num = 0 for video in os.listdir(args.input_file): img_num += 1 with open(os.path.join(args.input_file, video)) as fh: paths = filter(None, fh.readlines()) mime_type = "image/jpg" for path in paths: key = (os.path.join(args.remote_dir, path)).strip('\n') print 'upload ', path, ' as ', key # path = path.split()[0] localfile = (os.path.join(args.local_dir, '%s' % (path))).strip('\n') print localfile token = q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, mime_type=mime_type, check_crc=True) assert info.status_code == 200 assert ret['key'] == key assert ret['hash'] == etag(localfile) print '%d images uploaded' % (img_num)
def up2cloud(filename, abs_filepath, need_rm=True, pic_host=""): ckey = "up2cloud" thekey = filename rcli = RedisClient().get_cli() thetoken = rcli.get(ckey) if thetoken: thetoken = json.loads(thetoken) else: q = Auth(access_key, secret_key) # 上传后保存的文件名 # 生成上传 Token,可以指定过期时间等 ttl = 3600 thetoken = q.upload_token(g_bucket_name, None, ttl) rcli.set(ckey, json.dumps(thetoken), ex=ttl - 600) # 要上传文件的本地路径 ret, info = put_file(thetoken, thekey, abs_filepath) uri = "" if ret and ret["key"] == thekey and ret['hash'] == etag(abs_filepath): uri = json.loads(info.text_body)["key"] if not uri: g_stdlogging.error("[up2cloud]%s %s" % (ret, info)) raise unknown_err # print(abs_filepath) if need_rm: os.remove(abs_filepath) if not pic_host: pic_host = app.config["PIC_HOST"] return pic_host + "/" + uri
def main(): parser = argparse.ArgumentParser(prog='qiniu') sub_parsers = parser.add_subparsers() parser_etag = sub_parsers.add_parser( 'etag', description='calculate the etag of the file', help='etag [file...]') parser_etag.add_argument('etag_files', metavar='N', nargs='+', help='the file list for calculate') args = parser.parse_args() try: etag_files = args.etag_files except AttributeError: etag_files = None if etag_files: r = [etag(file) for file in etag_files] if len(r) == 1: print(r[0]) else: print(' '.join(r))
def token(access_key, secret_key, bucket_name, key): ''' :param access_key: 七牛账户的key默认为系统设置key如果不是可以重设! :param secret_key:七牛账户的secret_key默认为系统值 :param bucket_name: 要上传的空间 :param key: 上传到七牛后保存的文件名 ''' # 构建鉴权对象 q = Auth(access_key, secret_key) t = q.upload_token(bucket_name, key, 3600) return policy = { 'callbackUrl': 'http://your.domain.com/callback.php', 'callbackBody': 'filename=$(fname)&filesize=$(fsize)' } # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket=bucket_name, expires=3600, policy=policy) ret, info = put_file(token, key, file) print token print info assert ret['key'] == key assert ret['hash'] == etag(file) return 'http://7xiie2.com1.z0.glb.clouddn.com/' + key
def test_get_file_info(self): info = self.q.get_file_info('test.py') ret = json.loads(info.text_body) print(ret['putTime']) self.assertIsNotNone(info) self.assertIn('hash', ret) self.assertEqual(ret['hash'], etag('test.py'))
def save_file_to_s3(file_path, dirpath=None, public=False, dest_path=None): if settings.USE_S3_STORAGE: q = Auth(settings.QINIU_ACCESS_KEY, settings.QINIU_SECRET_KEY) token = q.upload_token(settings.QINIU_BUCKET_NAME, file_path, 3600) ret, info = put_file(token, file_path, file_path) assert ret['key'] == file_path assert ret['hash'] == etag(file_path)
def upload_img(self, img_file): img_url = None # 获取当前时间 time_now = int(time.time()) # 转换成localtime time_local = time.localtime(time_now) # 转换成新的时间格式(2016-05-09 18:59:20) ts = time.strftime("%Y%m%d%H%M%S", time_local) #要上传的空间 bucket_name = self.bucket_name #上传到七牛后保存的文件名 key = ts + '.png' #生成上传 Token,可以指定过期时间等 token = self.qny_auth.upload_token(bucket_name, key, 3600) #要上传文件的本地路径 local_file = img_file try: ret, info = put_file(token, key, local_file) # print(info) assert ret['key'] == key assert ret['hash'] == etag(local_file) img_url = self.outside_chain + '/' + key except Exception as e: print e return img_url
def qiniu_up(pic_name): from qiniu import Auth, put_file, etag, urlsafe_base64_encode import qiniu.config # 需要填写你的 Access Key 和 Secret Key access_key = '3WdBnUyD1kJmJEg9Tih3fzmcGoxYFUGBO9RtKJN7' secret_key = 'dFL1xftxFoTPSXUCc1tg3g9Ve0DafpHKsWABCmhg' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'python-web' # 上传到七牛后保存的文件名 key = pic_name # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 localfile = os.path.join(image_file_dir, pic_name) ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) # 返回外链domain return 'http://p5shjfo1t.bkt.clouddn.com/'
def qiniu_upload(file_path, bucket='mltalker', out_url_base='http://experiment.mltalker.com', max_size=10): return 'qiniu:%s/%s' % ('http:127.0.0.1', 'aaa') access_key = 'ZSC-X2p4HG5uvEtfmn5fsTZ5nqB3h54oKjHt0tU6' secret_key = 'Ya8qYwIDXZn6jSJDMz_ottWWOZqlbV8bDTNfCGO0' q = Auth(access_key, secret_key) if max_size is not None: # check file size fsize = os.path.getsize(file_path) fsize = fsize / float(1024 * 1024) if fsize > max_size: logger.error('file size is larger than limit (%dMB)' % max_size) return None key = file_path.split('/')[-1] token = q.upload_token(bucket, key, 3600) ret, info = put_file(token, key, file_path) if ret['key'] == key and ret['hash'] == etag(file_path): logger.info('success to upload') return 'qiniu:%s/%s' % (out_url_base, key) return None
def upload(self, src, dest, check=True): token = self.q.upload_token(self.bucket_name, dest, self.expire) ret, info = put_file(token, dest, src) if check: assert ret['key'] == dest, "ret['key'] <{}> unmatch dest <{}>".format(ret['key'], dest) assert ret['hash'] == etag(src), "ret['hash'] <{}> unmatch etag(src), <{}>".format(ret['hash'], etag(src)) return True
def sync2qiniu(filename, bkt, key, secret): import qiniu q = qiniu.Auth(key, secret) token = q.upload_token(bkt, filename, 3600) print('Send %s to qiniu ...' % filename) ret, info = qiniu.put_file(token, filename, filename) if (ret['key'] == filename) and (ret['hash'] == qiniu.etag(filename)): print('upload successful')
def putfile(file_url): q = Auth(access_key, secret_key) bucket = BucketManager(q) mime_type, file_type = get_mime_type(file_url) key = etag(file_url) + "." + file_type # "test" token = q.upload_token(bucket_name, key) ret, info = put_file(token, key, file_url, mime_type=mime_type, check_crc=True) print ret
def test_putfile(self): localfile = __file__ key = 'test_file' token = self.q.upload_token(bucket_name, key) ret = putfile(token, key, localfile, mime_type=self.mime_type, check_crc=True) assert ret['key'] == key assert ret['hash'] == etag(localfile)
def test_retry(self): localfile = __file__ key = "test_file_r_retry" qiniu.set_default(default_zone=Zone("http://a", "http://upload.qiniu.com")) token = self.q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, self.params, self.mime_type) print(info) assert ret["key"] == key assert ret["hash"] == etag(localfile)
def test_putfile(self): localfile = __file__ key = "test_file" token = self.q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, mime_type=self.mime_type, check_crc=True) print(info) assert ret["key"] == key assert ret["hash"] == etag(localfile)
def test_retry(self): localfile = __file__ key = 'test_file_r_retry' qiniu.set_default(default_zone=Zone('http://a', 'http://upload.qiniu.com')) token = self.q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, self.params, self.mime_type) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
def upload_file(self, key, localfile): # key = 'home/carlo/test_file.py' mime_type = "text/plain" params = {'x:a': 'a'} token = self.q.upload_token(self.bucket_name, key) ret, info = put_file(token, key, localfile, mime_type=mime_type, check_crc=True) # print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
def upload(self, key, localfile = None, expires = 3600, deleteAfterDays = 7): policy = {'deleteAfterDays': deleteAfterDays} print policy #token = self.q.upload_token(self.bucket_name, key, expires, policy, False) token = self.q.upload_token(self.bucket_name, key, expires, policy) ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
def upload_file(upload_file_name, temp): key = md5(str(time.time())+''.join(random.sample(string.letters, 12))).hexdigest() mime_type = 'image/png' token = q.upload_token(bucket, key) ret, info = put_file(token, key, upload_file_name, mime_type=mime_type, check_crc=True) print 'upload qiniu result:', info assert ret['key'] == key assert ret['hash'] == etag(upload_file_name) os.rename(upload_file_name, upload_file_name+'.old') return domain+'/'+key
def test_retry(self): localfile = __file__ key = 'test_file_r_retry' qiniu.set_default(default_up_host='a') token = self.q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, self.params, self.mime_type) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile) qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
def process_IN_MODIFY(self, event): click.echo('MODIFY event:%s' % event.pathname) key = self._qiniu_client.cal_key(event.pathname) stat = self._qiniu_client.stat(key) if stat is not None and stat['hash'] != qiniu.etag(event.pathname): self._qiniu_client.upload_file(event.pathname) bubble_notify = pynotify.Notification( "qiniubed", "上传{}成功".format(event.pathname)) bubble_notify.show() url = self._qiniu_client.get_chain(event.pathname) pyperclip.copy(url)
def upload(self, file_from: str, file_to: str, process_callback): if not self.__is_initialized: print("Have not been initialized!") return None print("Qiniu Ready Upload [%s]" % file_from) names = file_to.split("/") file_to = names[len(names) - 1] token = self.__auth.upload_token(self.__bucket_name, file_to) ret, info = put_file(token, file_to, file_from, progress_handler=process_callback) assert ret['key'] == file_to assert ret['hash'] == etag(file_from) return info
def qn_upload (q, bucket, pic): ret = False qn_pic_key = os.path.basename(pic) token = q.upload_token(bucket, qn_pic_key, 3600) qn_ret, qn_info = put_file(token, qn_pic_key, pic) if not qn_ret: print qn_info else: ret = (qn_ret['key'] == qn_pic_key and qn_ret['hash'] == etag(pic)) qn_pic_link = 'http://%s.qiniudn.com/%s' %(bucket, qn_pic_key) return ret, qn_pic_link
def down(token, key, localfile, mime_type, delete=False): ret, info = qiniu.put_file( token, key, localfile, mime_type=mime_type, check_crc=True ) assert ret['key'] == key assert ret['hash'] == qiniu.etag(localfile) print(localfile) if delete: os.remove(localfile)
def upload_file(self, filepath, file_key=None, mime_type=None): if file_key is None: file_key = path.basename(filepath) token = self._q.upload_token(self.bucket_name) ret, info = put_file(token, file_key, filepath, mime_type=mime_type, check_crc=True) #file_exists if ret and info.status_code == 614: return self.get_file_url(file_key) #upload success assert ret['key'] == file_key assert ret['hash'] == etag(filepath) return self.get_file_url(file_key)
def upload(local_file, remote_name, ttl=3600): print(local_file, remote_name, ttl) #构建鉴权对象 q = Auth(ACCESS_KEY, SECRET_KEY) #生成上传 Token,可以指定过期时间等 token = q.upload_token(BUCKET_NAME, remote_name, ttl) ret, info = put_file(token, remote_name, local_file, progress_handler=progress_handler) print(info) assert ret['key'] == remote_name assert ret['hash'] == etag(local_file)
def delete_photo_album(request,album_id,photo_id): if request.method == 'POST': f = request.FILES["uploader_input"] parser = ImageFile.Parser() for chunk in f.chunks(): parser.feed(chunk) img = parser.close().resize((100, 100),Image.ANTIALIAS) photo = Photo.objects.create() photo.save() # 在img被保存之前,可以进行图片的各种操作,在各种操作完成后,在进行一次写操作 path = str(photo.id)+'.jpg' filename = path img.save(path) #选择上传头像到七牛还是本地 try: #上传头像到七牛 from osf.settings import qiniu_access_key,qiniu_secret_key,qiniu_bucket_name import qiniu assert qiniu_access_key and qiniu_secret_key and qiniu_bucket_name q = qiniu.Auth(qiniu_access_key, qiniu_secret_key) key = filename localfile = path mime_type = "image/jpeg" params = {'x:a': 'a'} token = q.upload_token(qiniu_bucket_name, key) ret, info = qiniu.put_file(token, key, localfile, mime_type=mime_type, check_crc=True) #图片连接加上 v?时间 是因为七牛云缓存,图片不能很快的更新,用filename?v201504261312的形式来获取最新的图片 request.user.img = "http://7xjfbe.com1.z0.glb.clouddn.com/"+filename + "?v" + time.strftime('%Y%m%d%H%M%S') request.user.save() #print ret['key'],ret['hash'] #验证上传是否错误 if ret['key'] != key or ret['hash'] != qiniu.etag(localfile): logger.error(u'[UserControl]上传错误:[%s]' % request.user.username) return HttpResponse(u"上传错误",status=500) mydict = {"id":photo.id,"status":"107000","key":key} return HttpResponse(json.dumps(mydict),content_type="application/json") except Exception as e: #验证上传是否错误 if not os.path.exists(path): logger.error(u'[UserControl]用户上传出错:[%s]',request.user.username) return HttpResponse(u"上传错误",status=500) return HttpResponse(u"上传失败!\n")
def is_diff(key,localfile): ''' 比较本地文件与文件文件是否一致 ''' ret, info = bucket.stat(bucket_name, key) #print(ret,info) hash = ret if hash: diff = ret['hash'] != etag(localfile) # diff==False,说明文件相同,这个有点绕 return diff else: diff = True # 不存在hash,说明还没上传过 return True # 为True,说明文件对比结果为不一致
def upload_file(bucket_name, localfile, key=''): global access_key global secret_key global q if key=='': key = basename(localfile) bucket = BucketManager(q) token = q.upload_token(bucket_name, key, 3600) ret, info = put_file(token, key, localfile) #print(ret) #print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile) print("%s upload to qiniu"%basename(localfile))
def form_valid(self, form): self.object = form.save(commit=False) self.object.key = unicode(uuid.uuid4().get_hex()) self.object.author = MyUser.objects.get(id=1) # handle uploaded image if self.request.FILES.get('photo'): uploaded_image = self.object.photo thumb_file = StringIO() with Image(file=uploaded_image.file) as original: original.resize(280, 180) with original.convert('jpeg') as img: img.save(file=thumb_file) thumb_image = ContentFile(thumb_file.getvalue()) self.object.photo.name = unicode(uuid.uuid4())[:8] + u'.jpg' self.object.photo.file = thumb_image # 将图片保存到本地的media中 self.object.save() try: from flyingpang.settings import qiniu_access_key, qiniu_secret_key, bucket_name, qiniu_domain import qiniu if qiniu_access_key and qiniu_secret_key and bucket_name: q = qiniu.Auth(qiniu_access_key, qiniu_secret_key) # 上传本地文件 localfile = settings.BASE_DIR + self.object.photo.url print localfile key = self.object.photo.name[2:] # 文件名 mime_type = 'image/jpeg' # mimeType params = {'x:a': 'a'} token = q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, mime_type=mime_type, check_crc=True) assert ret['key'] == key assert ret['hash'] == etag(localfile) self.object.img_url = "http://" + qiniu_domain + '/' + self.object.photo.name[2:] self.object.save() except ImportError: pass self.object = form.save() return super(ModelFormMixin, self).form_valid(form)
def qn_up_img(file_path,uped_name): qiniu_access_key = "your qiniu access key" qiniu_secret_key = "your qiniu secret key" qiniu_bucket_name = "your qiniu bucket name" q = qiniu.Auth(qiniu_access_key,qiniu_secret_key) key = uped_name mime_type = "image/jpeg" params = {'x:a': 'a'} localfile = file_path token = q.upload_token(qiniu_bucket_name, key) ret, info = qiniu.put_file(token, key, localfile, mime_type=mime_type, check_crc=True) assert ret['key'] == key assert ret['hash'] == qiniu.etag(localfile)
def main(): parser = argparse.ArgumentParser(prog="qiniu") sub_parsers = parser.add_subparsers() parser_etag = sub_parsers.add_parser("etag", description="calculate the etag of the file", help="etag [file...]") parser_etag.add_argument("etag_files", metavar="N", nargs="+", help="the file list for calculate") args = parser.parse_args() if args.etag_files: r = [etag(file) for file in args.etag_files] if len(r) == 1: print(r[0]) else: print(" ".join(r))
def upload_file(self, key, filename): """上传文件到 CDN 参数: key: 储存在 CDN 上的文件名, 可以包含 "/", 访问时会有层级感. filename: 本地文件的路径 返回: 返回成功与否. """ token = self.__client.upload_token(self.__bucket, key) ret, info = qiniu.put_file( token, key, filename, mime_type='image/jpeg', check_crc=True ) return ret['key'] == key and ret['hash'] == qiniu.etag(filename)