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 upload(key): access_key = 'Fx9CvaSHqUFVrofBgpGxAdsR3UV0SNB2bRof6ss2' secret_key = 'eW8fX00uUZukKcFsZyeIWw3BTyfhyoIhGQ4j5NwN' q = Auth(access_key, secret_key) bucket_name = 'bing' token = q.upload_token(bucket_name, key, 3600) localfile = key put_file(token, key, localfile)
def user_home(request): user = get_user(request) if request.method == 'GET': data = {'user': user} return render(request, 'home/user/user_home.html', data) if request.method == 'POST': username = request.POST.get('username') icon = request.FILES['icon'] email = request.POST.get('email') sex = request.POST.get('sex') tel = request.POST.get('tel') if username and username != user.username: if User.objects.filter(username=username).first(): msg = '用户名已存在' return render(request, 'home/user/user_home.html', {'msg': msg}) user.username = username if email and email != user.email: if User.objects.filter(email=email).first(): msg = '该邮箱已注册' return render(request, 'home/user/user_home.html', {'msg': msg}) if not re.match("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$", str(email)): msg = '请输入有效邮箱号' return render(request, 'home/user/user_home.html', {'msg': msg}) user.email = email if icon: with open(BASE_DIR + AVATAR_DIR, 'wb') as pic: for c in icon.chunks(): pic.write(c) # 构建鉴权对象 q = qiniu.Auth(ACCESS_KEY, QINIU_SECRET_KEY) # 上传到七牛后保存的文件名 key = 'static/icon/' + user.username + '/'+str(time.time())+'.jpg' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(BUCKET_NAME, key) localfile = BASE_DIR + '/static/images/icon.jpg' qiniu.put_file(token, key, localfile) user.icon = 'http://pbu0s2z3p.bkt.clouddn.com/' + key if tel and tel != user.tel: if User.objects.filter(tel=tel).first(): msg = '该手机已注册' return render(request, 'home/user/user_home.html', {'msg': msg}) if not re.match("^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$", str(tel)): msg = '请输入有效手机号' return render(request, 'home/user/user_home.html', {'msg': msg}) user.tel = tel if sex and sex != user.sex: user.sex = 1 if sex == '男' else 0 user.save() return HttpResponseRedirect('/user/user_home/')
def __upload_file(self,localphoto): localfile = localphoto.getpath() key = self.pre + localfile.split('\\')[-1] inf_key = key + '&&' + str(localphoto.getmood())\ +'&&'+localphoto.getcomment()+'&&.inf' self.inf[key] = [localphoto.getmood(),localphoto.getcomment] token1 = self.q.upload_token(self.BN,inf_key) token = self.q.upload_token(self.BN, key) mime_type = self.__get_mime_type(localfile) params = {'x:a': 'a'} progress_handler = lambda progress, total: progress ret1, info1 = qiniu.put_file(token1, inf_key, self.static_file,params,\ mime_type, progress_handler=progress_handler) ret, info = qiniu.put_file(token, key, localfile, params,\ mime_type, progress_handler=progress_handler)
def loop_qiniu(): while True: for f in os.listdir(UPLOAD_FOLDER): if 'tmp' not in f.split('_')[-1]: os.remove(os.path.join(UPLOAD_FOLDER, f)) # print 'jump out and for one more time' elif 'tmp0' == f.split('_')[-1]: continue elif 'tmp' == f.split('_')[-1]: print 'start handle %s' % f f_path = os.path.join(UPLOAD_FOLDER, f) f_path_new = os.path.join(UPLOAD_FOLDER, f[:-4]) key = f[:-4] q = Auth(access_key, secret_key) token = q.upload_token(Bucket, key, 3600) local_file = f_path ret, _ = put_file(token, key, local_file) try: assert ret['key'] == key except TypeError: print 'ret has error' continue os.rename(f_path, f_path_new) print 'end handle %s' % key print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time())) else: print '<----' print u'为考虑到的情况' print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time())) print '---->' print 'pass this time while error', time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
def qiniuUpdate(url): #需要填写你的 Access Key 和 Secret Key access_key = '8qXT7YOMZ-GtjM36rtkzKMEuZSaDrtbSPetdXYIf' secret_key = 'zbp-eUwRuMzucnYr37u_zXyNsiKkxBrTB84CmmSu' #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = 'com-yixiantong-yxkj' #mutex = threading.Lock() #上传到七牛后保存的文件名 keyVal = str(uuid.uuid1()) keyTime = str(int(time.time() * 1000)) + '-' key = keyTime + keyVal[0:8] #mutex.acquire(3) #生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) file_name = urllib2.unquote(url).decode('utf8').split('/')[-1] #print(file_name) f = urllib2.urlopen(url) with open(file_name, "wb") as code: code.write(f.read()) time.sleep(1) #urllib.urlretrieve(url, file_name) #要上传文件的本地路径i localfile = '/app/yxtk/script/' + file_name if os.path.exists(localfile): ret, info = put_file(token, key, localfile) return 'http://7xpkhu.com2.z0.glb.qiniucdn.com/' + key else: return url
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 openpic(request): from qiniu import Auth, put_file, etag import qiniu.config from PIL import Image from aip import AipImageCensor a = request.FILES.get("imageData") #得到图片 b = random.randint(111111, 999999) key = 's2' + str(b) + '.jpg' Image = Image.open(a) Image.save('s1.jpg') #保存本地 APP_ID = '10027473' API_KEY = 'vuVlmlKsXULFfo438jiWfxb0' SECRET_KEY = '33Iew8K4zT3hBBI1YqGPaK9vcHD5dAxG' client = AipImageCensor(APP_ID, API_KEY, SECRET_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() img = get_file_content('s1.jpg') result = client.imageCensorUserDefined(img) if result['conclusion'] == '合规': # path = default_storage.save('s1.jpg', ContentFile(Image.read())) access_key = 'EjIqvG2iluFxOx-PzeIyCNUKqUL2je9Q5bfunOyg' secret_key = 'pqt97zqOt7cw07a9AWwEXPp0zofB_9swvTVJGOOr' q = Auth(access_key, secret_key) bucket_name = 'xunyingpicture' localfile = r"s1.jpg" token = q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile) #返回图片地址 return HttpResponse('http://files.g8.xmgc360.com/' + key) else: return HttpResponse("1")
def put_up_datum(file_path, key, kind="file", progress_handler=None): """上传资料, 三种模式: data, file, stream :parameter file_path :parameter key :parameter kind :parameter progress_handler """ up_token = get_up_token(key) if kind == "data": with open(file_path, 'rb') as input_stream: data = input_stream.read() ret, info = put_data(key=key, data=data, check_crc=True, up_token=up_token, mime_type="application/octet-stream", progress_handler=progress_handler) elif kind == "stream": size = os.stat(file_path).st_size with open(file_path, 'rb') as input_stream: ret, info = put_stream(key=key, up_token=up_token, input_stream=input_stream, data_size=size, progress_handler=progress_handler) else: ret, info = put_file(key=key, up_token=up_token, file_path=file_path, progress_handler=progress_handler) return ret, info
def post(self, request): try: pt = request.POST.get('path') a_id = request.POST.get('user_id') bd_id = request.POST.get('only_id') # cho = request.POST.get('choice_classfiy') access_key = dev.QI_NIU_ACCESS_KEY secret_key = dev.QI_NIU_SECRET_KEY q = Auth(access_key, secret_key) # 构建鉴权对象 bucket_name = dev.QI_NIU_BUCKET_NAME # 要上传的空间 new_uu = str(uuid.uuid4()) suid = ''.join(new_uu.split('-')) path = str(pt) key = str(a_id) + '_' + str(suid) token = q.upload_token(bucket_name, key, 3600) ret, info = put_file(token, key, path) assert ret['key'] == key assert ret['hash'] == etag(path) url = 'http://tp.kan3721.com/' + ret['key'] os.remove(path) return JsonResponse({'token': token, 'CODE': '200', 'url': url}) except Exception as e: context = {'CODE': '400', 'ERROR': {e}} return JsonResponse(context)
def upload(filepath): qiniu_domain = 'http://7xtq0y.com1.z0.glb.clouddn.com/' #需要填写你的 Access Key 和 Secret Key access_key = 'NhxOewLDWpAs_THJNvtKN8kZHG3r0_tkWOaJSycc' secret_key = 'Cx7AJItEyNWaEG1eLvu85PpCGq0vAaX1xmvJC7c0' #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = 'images' #上传到七牛后保存的文件名 key = os.path.basename(filepath) qiniu_url = qiniu_domain + key #生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) #要上传文件的本地路径 localfile = filepath ret, info = put_file(token, key, localfile) print(info) print('===============') print(qiniu_url) return qiniu_url
def Upload(dsthost): # Access Key Secret Key num = random.randint(0, 99) access_key = 'XXXXX' secret_key = 'XXXXX' # 构建鉴权对象 q = Auth(access_key, secret_key) # 上传的空间name bucket_name = 'XXXXX' # 保存的文件名 key = dsthost + '_' + timea + 'report' + '.html' print(key) # key = '1.png' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 # localfile = '/usr/script/report/1.png' localfile = '/usr/script/report/' + dsthost + '_' + timea + '_report.html' ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile) yuming = 'XXXXX' #your domain name fileurl = yuming + '/' + key return fileurl
def upload_qiniu(file_name, file_path, user_id): # 2.调用七牛云sdk上传 # 需要填写你的 Access Key 和 Secret Key access_key = 'KXfx2ZiBP311HkZZ8l8JHCmqlqPTJCK2sraiheVU' secret_key = 'pe5svTTUAJQoUPhppsf0Gg9wbEJiYahnRMAy1rgP' # 要上传的空间 bucket_name = 'liu0330' # 要上传的域名 bucket_domain = ' liu0330.s3-cn-south-1.qiniucs.com' # 构建鉴权对象 q = Auth( access_key, secret_key, ) token = q.upload_token(bucket_name, file_name, 3600) ret, info = put_file(token, file_name, file_path) print(info) #断言 assert ret['key'] == file_name assert ret['hash'] == etag(file_path) print('save qiniu ok.') avatar_url = f'{bucket_domain}/{file_name}' # 3.更新用户的avatar user = User.objects.get(id=user_id) # user = request.user # request 无法传 所以改为user_id user.avatar = avatar_url user.save() return True
def get_img_url(local_path): """ 将图片传到七牛并获取外链 :param local_path: 本地绝对路径 :return: """ # 需要填写你的 Access Key 和 Secret Key access_key = 'eIugjZLSl5-CSSQaZcGDkqL5A2q3ZBkbOBKX8w_v' secret_key = 'WpI4jjPhx6U0AKPD5v0liLFzehQ0qhqUDQJL5HEr' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'pandatv' # 要上传文件的本地路径 localfile = local_path # 上传到七牛后保存的文件名 key = uuid.uuid4().hex + '.png' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket=bucket_name, key=key) ret, info = put_file(token, key, localfile) print(localfile.split('/')[-1], key, info) # print( assert ret['key'] == key assert ret['hash'] == etag(localfile) return {'url': 'http://pe41pg10l.bkt.clouddn.com/' + key}
def upload_img(bucket_name,file_name,file_path): # generate token token = q.upload_token(bucket_name, file_name, 3600) info = put_file(token, file_name, file_path) # delete local imgFile os.remove(file_path) return
def to_upload_file(self, f_file, f_md5, f_type): ''' to upload file into qiniu bucket ''' try: # f_name = os.path.basename(f_file) q = qiniu.Auth(self.access_key, self.secret_key) # key means filename in qiniu bucket key = "%s%s.%s" % (self.image_prefix, f_md5, f_type) token = q.upload_token(self.bucket_name, key) qiniu.put_file(token, key, f_file) return True except: return False
def upload_file(self, bucket_name, key, file_path): #生成上传 Token,可以指定过期时间等 token = self._q.upload_token(bucket_name, key, 3600) ret, info = put_file(token, key, file_path) print(info) pic_url = self.get_pic_url(bucket_name, file_path) add_to_clipboard(pic_url, file_path)
def upload_photo(): """上传照片到七牛,并返回私有链接地址""" from qiniu import Auth from qiniu import put_file global config progress_handler = lambda progress, total: progress photo_path = http_get("http://127.0.0.1:9876/photo/shot").content # Upload to qiniu mime_type = "image/jpeg" auth = Auth(str(config["qiniu"]["api_key"]), str(config["qiniu"]["secret"])) print auth filename = os.path.basename(photo_path) print "filename: ", filename, type(filename) token = auth.upload_token(str(config["qiniu"]["bucket"])) print token ret, info = put_file(token, filename, photo_path, {}, mime_type, progress_handler=progress_handler) print "uploaded: ", ret, info try: os.remove(photo_path) except Exception: pass # Return URL base_url = "{}/{}".format(str(config["qiniu"]["domain"]), filename) return auth.private_download_url(base_url, expires=3600)
def upload_without_key(bucket, filePath, uploadname): '''上传文件''' auth = qiniu.Auth(accessKey, secretKey) upToken = auth.upload_token(bucket, key=None) key = uploadname retData, respInfo = qiniu.put_file(upToken, key, filePath, mime_type=mime.guess_type(filePath)[0]) parseRet(retData, respInfo)
def put(self, localfile): name = os.path.split(localfile)[1] key = self.__prefix + name + '.gif' token = self.__q.upload_token(self.__bucket, key) ret, info = put_file(token, key, localfile) print(ret, info) return self.__domain + key
def qiniu_upload_file(source_file, save_file_name): token = q.upload_token(bucket_name, save_file_name) ret, info = put_file(token, save_file_name, source_file.stream) if info.status_code == 200: return bucket_domain + save_file_name else: return None
def up_img(self, img_url, img_name): """ 图片上传 Parameters: img_url:图片地址 img_name:图片名称 :return: """ access_key = self.public_key secret_key = self.private_key # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = self.bucket # 上传到七牛后保存的文件名 key = "zx/" + img_name # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 # video_url = 'C:/Users/rwcym/Desktop/农村小鹏777/6572143556763323661-这么小就能看出,以后肯定有出息,这吃相没谁了!.mp4' ret, info = put_file(token, key, img_url) # 七牛云的url qny_name = "http://img.supercr.top/" + ret['key'] self.logger.info('七牛云上传完毕 %s \n' % qny_name) return qny_name
def qiniu_upload(self): # 构建鉴权对象 q = Auth(self.quniu_access_key, self.qiniu_secret_key) # 要上传的空间 bucket_name = self.qiniu_bucket_name # 上传到七牛后保存的文件名 file_path = self.FilePath key = file_path.split("/")[-1] # 生成上传 Token,可以指定过期时间 token = q.upload_token(bucket_name, key, 3600) # 更新状态栏 self.add_log('File uploading') ret, info = put_file(token, key, file_path) assert ret['key'] == key assert ret['hash'] == etag(file_path) self.GetFilePath = self.qiniu_get_file_path + key self.add_log('Qiniu File upload success ' + self.GetFilePath) self.copytext()
def icon_modify(request): if request.method == 'POST': try: file = request.FILES.get('file') file_ex = file.name.split('.')[-1] media_dir = os.path.join(os.path.join(BASE_DIR, 'static/media'), 'image.' + file_ex) with open(media_dir, 'wb') as f: for chunk in file.chunks(): f.write(chunk) access_key = 'rdNJE5t51H_Y8MDgF_Cg4d1q5fjVQvvR6dDgIHnY' secret_key = 'DBSZaWdYKsfqvRaAqvRkPpGtcD-XiLDmsYnFBIZj' bucket_url = 'pbfkv2zhm.bkt.clouddn.com' # 仓库域名 bucket_name = 'hodge' # 仓库名 image_name = uuid.uuid1() q = qiniu.Auth(access_key, secret_key) # 验证 token = q.upload_token(bucket_name, image_name) ret, info = qiniu.put_file(token, image_name, media_dir) path = 'http://{}/{}'.format(bucket_url, ret['key']) User.objects.filter(pk=request.session['user_id']).update( head_icon=path) data = {'code': 200, 'path': path} return JsonResponse(data) except Exception as e: print(e) data = {'code': 400} return JsonResponse(data)
def update_icon(): """""" icon = request.files['icon'] site_id = request.form.get('id', '') conf = current_app.config if not icon or not site_id: return '' filename = randstr(16) filepath = os.path.join(conf['ICON_DIR'], "{}.png".format(filename)) icon.save(filepath) if not os.path.exists(filepath) or os.stat(filepath).st_size > 10 * 1024: return '' key = 'site/{}.png'.format(filename) q = Auth(conf['QINIU_ACCESS_KEY'], conf['QINIU_ACCESS_SECRET']) token = q.upload_token(conf['QINIU_BUCKET'], key, 60) ret, info = put_file(token, key, filepath) logging.info(info) site = UserSite.select(lambda x: x.id == site_id and x.user == request.user and x.status == UserSiteStatus.normal).first() if not site: return jsonify({'status': -1}) site.icon = filename commit() return ''
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_url2file(self,s_url,f_name): if not os.path.exists('./temp'): os.makedirs('./temp') img = requests.get(s_url) # print(img.content) localfile = "./temp/"+f_name fp = open(localfile, 'ab') fp.write(img.content) fp.flush() fp.close() # 构建鉴权对象 q = Auth(self.access_key, self.secret_key) # 要上传的空间 bucket_name = 'knightcomment' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, f_name, 3600) # 要上传文件的本地路径 ret,info = put_file(token, f_name, localfile) print("response:", ret) print("response:", info) # ret_dic = json.loads(info) status = False if info.status_code == 200: status = True os.remove(localfile) return status
def getImg(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') for img in images: link = img.get('src') #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 img['src'] = newUrl print(newUrl) print(soup) return str(soup)
def uploadToQiNiu(filePath,name): q = Auth(access_key, secret_key) localfile = filePath key = name mime_type = "image/jpeg" token = q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, mime_type=mime_type, check_crc=True)
def test_upload(): q = Auth(access_key, secret_key) token = q.upload_token(bucket, mykey.encode('utf-8')) file = '/tmp/abc.txt' ret, info = put_file(token, mykey.encode('utf-8'), file, mime_type="text/plain", check_crc=True) print(info) print(ret)
def _upload(id): try: from qiniu import Auth, put_file, etag, urlsafe_base64_encode import qiniu.config import time access_key = '6GXfBrWJbYN3N2y0zuMX7rf7tmvoqWg5CFwMYMKh' secret_key = 'Nk9DmZ95PRqqH59-2FH6__q5e5CQXgNKZj2LfBf2' #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = 'gparraycontroller' #上传到七牛后保存的文件名 key = str(id) + '.' + str(time.time()) + '.jpg' #生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) #要上传文件的本地路径 localfile = './output/' + str(id) + '.jpg' ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile) return '<img src="http://omm25myif.bkt.clouddn.com/{}?imageView2/1/w/200/h/150/format/jpg/interlace/1/q/75|imageslim"><br/>'.format( key) except Exception as e: return 'Error'
def upload_qiniu(file_name, file_path, user_id): # 调用七牛云的 SDK 上传 access_key = 'cqxGEzAS6TSU_996r3svOLytJu1hQWU3Eb7oT3PM' secret_key = 'udaYEEpShey2UdioqDQuA9nACCKIeSqvQNccaja0' bucket_name = 'joker123' bucket_domain = 'q3h2ql4e2.bkt.clouddn.com' # 构建鉴权对象 q = Auth(access_key, secret_key) # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, file_name, 3600) # 上传文件 ret, info = put_file(token, file_name, file_path) print('upload ok.') # 更新用户的 avatar 字段 avatar_url = f'{bucket_domain}/{file_name}' user = User.objects.get(id=user_id) user.avatar = avatar_url user.save() print('all done.') return True
def process_item(self, item, spider): if self.ak: for field in self.fields: try: val = item[field] except: # noqa pass else: if val.startswith('./'): val = val[2:] q = Auth(self.ak, self.sk) token = q.upload_token(self.bucket, val, 3600, policy=self.policy) ret, info = put_file(token, val, val) if info.status_code == 200: item[field] = f'http://{self.domain}/{val}' logging.debug( f"success to put file to qiniu: ret={ret} info={info}" ) public_url = q.private_download_url(item[field], 3600) logging.debug(f'public url is: {public_url}') else: logging.error( f"fail to put file to qiniu: ret={ret} info={info}" ) return item
def qi_local_upload(file_url): #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = 'prowait' #上传到七牛后保存的文件名 # key = 'my-python-logo.png' #生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, None, 3600) #要上传文件的本地路径 # localfile = './sync/bbb.jpg' # 直接put_data二进制存入 ret, info = put_file(token, None, file_url) # assert ret['key'] == key # assert ret['hash'] == etag(localfile) if info.status_code == 200: # 上传成功 返回文件名 默认七牛定义的哈希值 print(ret.get('key')) return ret.get('key') else: # raise Exception('七牛云上传失败') return 'err'
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 qiniu_upload(qiniu, **kwargs): """ 文件上传 :param kwargs: localfile_path:要上传的本地服务器文件路径 filename:文件名, 如果带上"/"则会创建对应的子目录,如post-img/xxxx-xxx-xxx.jpg prefix: 文件名前缀 :return: """ localfile_path = kwargs.get("localfile_path") filename = kwargs.get("filename") prefix = kwargs.get("prefix") if prefix: filename = "{}{}".format(prefix, filename).replace("//", "/") # 生成上传 Token,可以指定过期时间等 token = qiniu.upload_token(get_plugin_config(PLUGIN_NAME, "BUCKET_NAME"), filename, 3600) ret, info = put_file(token, filename, localfile_path) assert ret['key'] == filename assert ret['hash'] == etag(localfile_path) result = { "key": filename, "type": "qiniu", "bucket_name": get_plugin_config(PLUGIN_NAME, "BUCKET_NAME") } return result
def store_file(self, file_path, file_name): upload_token = self.credentials.upload_token(self.bucket_name, file_name) ret, err = put_file(upload_token, file_name, file_path) if ret is not None: return "%s/%s" % (self.imageServerUrl, ret['key']) else: logging.error('upload: %s error.' % file_name)
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 upload_qiniu(url, supplier_book_id): path = './images/%s.jpg' % supplier_book_id print(path) f = open(path, 'wb') # 注意第二个参数要写成wb,写成w会报错 req = urllib.request.urlopen(url) buf = req.read() # bufstr = buf.decode('utf-8','ignore') f.write(buf) # 需要填写你的 Access Key 和 Secret Key access_key = 'lDXeAAdaPJ2eSqI6c6qm_QR2LMMzx8lMtOdEg47z' secret_key = 'CPlluEcx-sVBTfzoOekWlU2fI158lypf1yow6QZ8' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'juhe-img' # 上传到七牛后保存的文件名 key = 'book_m1_%s' % supplier_book_id # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 localfile = path ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) qiniu_path = "http://odukcaoes.bkt.clouddn.com/%s" % (key) req.close() # 注意关闭response return qiniu_path
def qiniu_upload(token, key, pic): #res = qiniu_token(file_name) ret, info = put_file(token, key, pic) if ret['key'] == key: return {"result" : True, "file_name" : key} else: return {"result" : False, "file_name" : ""}
def upload_file(self, filename, filepath): """上传本地文件""" token = self.auth.upload_token(self.bucket, filename) ret, info = put_file(token, filename, filepath) if info.exception is not None: raise UploadError(info)
def upload(self, pro_id, shop_id, img_type, f): if not pro_id or not shop_id or not img_type: return -1, '未传入userid或businesstype', (0,0) folder_path = os.path.join(self.folder, img_type) if not os.path.exists(folder_path): os.mkdir(folder_path) new_fname = self.gen_img_name(pro_id, img_type, f) new_fpath = os.path.join(folder_path, new_fname) im = Image.open(f) size = im.size if size[0] > 1000: w = size[0] h = size[1] size = (1000, int(h/w * 1000)) nim = im.resize(size, Image.ANTIALIAS) nim.save(new_fpath, 'JPEG') size = nim.size token = self.get_token() ret, resp = put_file(token, new_fname, new_fpath, mime_type='image/jpeg', check_crc=True) if int(resp.status_code) == 614: print('已上传过,无需重复上传') return 614, '%s%s' % (self.url, new_fname), size elif int(resp.status_code) == 200: print("上传成功") return 200, '%s%s' % (self.url, new_fname), size else: print('访问出错,code:%s' % resp.status_code ) return -1, resp.error, (0,0)
def upload_content_img_to_qiniu(content): """ content参数是文章的html 该函数会将Ueditor的图片上传到七牛,并修改图片的src属性 最后返回修改过的content给QiniuUEditorField的get_prep_value调用 """ page = etree.HTML(content) hrefs = page.xpath("//img") imgs_src = [href.attrib["src"] for href in hrefs] for img_src in imgs_src: if settings.QINIU_BUCKET_DOMAIN not in img_src: img_name = "/" + img_src.split("/")[-1] file_path = settings.MEDIA_ROOT + img_name qiniu_key = "/".join(img_src.split("/")[1:]) try: ret, info = put_file(up_token=get_qiniu_uptoken(key=qiniu_key), key=qiniu_key, file_path=file_path) if info.exception is None: qiniu_file_path = "http://" + settings.QINIU_BUCKET_DOMAIN + img_src content = content.replace(img_src, qiniu_file_path) print img_name + " upload to qiniu success " else: print img_name + " upload to qiniu success " except Exception, e: print str(e) print " upload to qiniu failed "
def upload(self, data): """ :param data: { "filename":"", "filepath":"", "policy":{} } :return: ret info """ filename = data["filename"] filepath = data["filepath"] policy = data.get("policy", {}) if policy: token = self.authed.upload_token(BucketName, filename, Expired, policy) else: token = self.authed.upload_token(BucketName, filename, Expired) ret, info = put_file(token, filename, filepath) return ret, info
def upload_img(bucket_name,file_name,file_path): # generate token token = q.upload_token(bucket_name, file_name, 3600) info = put_file(token, file_name, file_path) # delete local img file and if you do not want to delete local img,you just need to delete "os.remove(file_path)" os.remove(file_path) return
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 set_head_picture(): ''' 图片名存储到数据库与七牛 ''' user = g.current_user # 获得图片的七牛云图片 # import ipdb; ipdb.set_trace() head_picture = get_qiniu_token(user.phone_number).json code, key, token = get_qiniu_token(user.phone_number).json.values() # 用户提交的图片 up_head_picture = request.values.get('head_picture') head_picture = 'http://pntn3xhqe.bkt.clouddn.com/{0}'.format(key) user.head_picture = head_picture # 图片上传 localfile = r'{0}'.format(up_head_picture) ret, info = put_file(token, key, localfile) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return jsonify({'code': 0, 'message': '未能成功上传'}) current_app.redis.hset('user:{0}'.format(user.phone_number), 'head_picture', head_picture) return jsonify({'code': 1, 'message': '成功上传'})
def upload_model(request): if request.method == 'POST': file = request.FILES.get('file') folder_id = int(request.POST.get('folder_id')) folder_name = folder.objects.get(id=folder_id).folder_name # print(folder_name) file_type = file.name.split('.')[1] if file_type != 'STL' and file_type != 'stl': return HttpResponse(file.name + '上传失败,因为文件格式不是STL') # file_name_url=str(round(time.time()*1000000))+'.STL' # file_path = os.path.join(os.path.dirname(globals()["__file__"]),'static','models',folder_name,file_name_url) # f = open(file_path, 'wb') # for chunk in file.chunks(): # f.write(chunk) # f.close() # # return HttpResponse('OK') # com_model.objects.create(model_name=file.name,folder_id=folder_id,url='/static/models/'+folder_name+'/'+file_name_url) # file_name_url='test'+'.STL' file_path = os.path.join(os.path.dirname(globals()["__file__"]), 'static', 'models', 'test.STL') f = open(file_path, 'wb') for chunk in file.chunks(): f.write(chunk) f.close() access_key = 'VfUZy5Gm-aQkbLkpm_lcTraFLW9ac9h1wj-SHbbr' secret_key = 'hBwXWe0BBbkkntfGRUtSEmsA1M9uZqrESiWyIzzk' #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = 'hugosmodel' #上传后保存的文件名 key = file.name.split('.')[0] + "(" + str(round( time.time() * 1000000)) + str(random.randint(0, 10000)) + ').STL' # key = 'qqqq' + '.STL' #生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) #要上传文件的本地路径 localfile = os.getcwd() + '/vmm/static/models/test.STL' ret, info = put_file(token, key, localfile) print(info.text_body) assert ret['key'] == key assert ret['hash'] == etag(localfile) com_model.objects.create(model_name=key, folder_id=folder_id, url='http://hugosmodel.diandijiaoyu.com.cn/' + key) return HttpResponse(key + '上传成功')
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 _upload_file(self, key, file_path): params = {'x:a': 'a'} upToken = self._auth.upload_token(self._bucket_name, key=key) ret, _ = qiniu.put_file(upToken, key, file_path, params=params, mime_type=mime.guess_type(file_path)[0]) return ret
def upload_file(self): token = self.q.upload_token(self.bucket, self.key, 60 * 60) try: put_file(token, self.key, file_path) except Exception as e: print('>>>', e) return choice = input( '>>>文件上传成功请选择你想要的图片返回格式(默认返回 http 格式)🚀:\n1. 返回 http 格式链接🔗\n2. 返回Markdown格式链接🔗\n' ) file_url = os.path.join(self.domain, self.key) if choice and int(choice) == 2: markdown_format_url = '![{0}][{1}]'.format(self.key, file_url) print(markdown_format_url) else: print(file_url)
def upload_qiniu(local_url, file_name, bucket_name=settings.QINIU_BUCKET): qiniu = Auth(settings.QINIU_ACCESS_KEY, settings.QINIU_SECRET_KEY) upload_folder = os.path.join(settings.QINIU_FOLDER, datetime.now().strftime('%Y%m%d') + '/') upload_name = upload_folder + file_name token = qiniu.upload_token(bucket_name, upload_name, 3600) ret, info = put_file(token, upload_name, local_url) return ret, info
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 upload_qiniu(path, upload_name): ''' upload file to qiniu''' q = Auth(config['ak'], config['sk']) key = upload_name # upload to qiniu's markdown dir token = q.upload_token(config['bucket'], key) ret, info = put_file(token, key, path, check_crc=True) return ret != None and ret['key'] == key
def upload(prefix, key, localpath): from qiniu import Auth, put_file q = Auth(ACCESS_KEY, SECRET_KEY) key = os.path.join(prefix, key) token = q.upload_token(BUCKET_NAME, key) ret, info = put_file(token, key, localpath)
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 upload(q, bucket_name, local_path, remote_path): bucket = BucketManager(q) assert bucket.stat(bucket_name, '')[1].status_code != 401 for rel_path in list_file(local_path): key = join(remote_path, rel_path) stat, _ = bucket.stat(bucket_name, key) if not stat: token = q.upload_token(bucket_name, key, 3600) log.info('uploading new file %s' % rel_path) put_file(token, key, join(local_path, rel_path)) elif stat['fsize'] != getsize(join(local_path, rel_path)) \ or stat['hash'] != etag(join(local_path, rel_path)): token = q.upload_token(bucket_name, key, 3600) log.info('uploading diff file %s' % rel_path) put_file(token, key, join(local_path, rel_path)) else: log.debug('file %s is identical' % rel_path)