def set_bucket_referer(): """ 对一个 bucket 设置防盗链,只需运行一次 """ acl_referer = '{"statements":[{"action":["*"],"effect":"allow","resource":["%s\\/"],"user":["*"],"referer":["http:\/\/%s\/*"]}]}' % (BUCKET, MAJOR_DOMAIN) from bae.api import bcs mybcs = bcs.BaeBCS(BCSHOST, AK, SK) mybcs.set_acl(BUCKET, "", acl_referer)
def get(self, sub_path): self.set_header('Content-Type', 'text/xml; charset=utf-8') from bae.core import const from bae.api import bcs import base64 baebcs = bcs.BaeBCS(const.BCS_ADDR, const.ACCESS_KEY, const.SECRET_KEY) e, response = baebcs.get_object(u'fashionramlab', r'/uploadimg/' + sub_path) self.write(base64.b64decode(response))
def upload_bcs(bucketname=BUCKET, savename="/test.txt", filedata=None): if bucketname and savename and filedata: if DEBUG: return None from bae.api import bcs mybcs = bcs.BaeBCS(BCSHOST, AK, SK) e,r = mybcs.put_object(bucketname, savename, filedata) if r is None: return "http://%s/%s%s" % (BCSHOST, bucketname, savename) else: return False else: return False
def transfer_file(flask_file): filename = flask_file.filename filename = filename[filename.rfind('/') + 1:] if not filename or not allowed_filename(filename): return None baebcs = bcs.BaeBCS(HOST, AK, SK) remote_name = str(BCS_PATH + str(time.time()) + '.' + filename) filedata = flask_file.read() ## 上传到BCS baebcs.put_object(BUCKET, remote_name, filedata) return SCHEMA + HOST + '/' + BUCKET + remote_name
def get(self): self.set_header('Content-Type', 'text/xml; charset=utf-8') from bae.core import const from bae.api import bcs username = self.get_secure_cookie('username') if username is not None: account_id = weicbd.aaa.AAADao().get_user(username)[0] import base64 baebcs = bcs.BaeBCS(const.BCS_ADDR, const.ACCESS_KEY, const.SECRET_KEY) e, response = baebcs.list_objects(u'fashionramlab', r'/uploadimg/' + str(account_id)) self.write('<images>') for result in response: self.write('<image>%s</image>' % result) self.write('</images>') else: self.write('<images/>')
def upload(request, studentid): filename = str(request.FILES['Filedata'].name) ext = filename.split('.')[-1] HOST = const.BCS_ADDR AK = const.ACCESS_KEY SK = const.SECRET_KEY bname = 'mediaavatar' baebcs = bcs.BaeBCS(HOST, AK, SK) data = request.FILES['Filedata'].read() img = Image.open(StringIO(data)) img = img.resize((70, 70), Image.ANTIALIAS) img = img.convert('RGB') data = StringIO() img.save(data, 'jpeg') try: del_object(bname, '/' + str(studentid) + '.jpg') except: pass baebcs.put_object(bname, '/' + str(studentid) + '.jpg', data.getvalue()) return HttpResponse(json.dumps(MEDIA_URL + str(studentid) + '.jpg'))
def upload(): if request.method == "POST": photo = request.files["uploaded_file"] if photo is None: return json.dumps({ 'code': 403, 'message': 'FILE IS EMPTY', 'data': 'null' }) else: if photo and allowed_file(photo.filename): filename_random = random_string() file_extension = photo.filename.rsplit('.', 1)[1] filename = '%s.%s' % (filename_random, file_extension) filedata = photo.read() filepath = UPLOAD_FOLDER + filename # bae bcs bbcs = bcs.BaeBCS(HOST, AK, SK) bbcs.put_object(BUCKET_NAME, str(filepath), filedata) # bae bcs return json.dumps({ 'code': 0, 'message': 'OK', 'data': 'http://%s/%s%s' % (HOST, BUCKET_NAME, filepath) }) else: return json.dumps({ 'code': 1, 'message': 'NOT FILE TYPE.', 'data': 'null' }) else: return ''
app.config['MAIL_USERNAME '] = "test" app.config['MAIL_PASSWORD '] = "test" from flask_mail import Mail mail = Mail(app) elif RUNTIME_ENV in ("gae", "gae_dev"): pass ##################################### ## Image Upload ##################################### if RUNTIME_ENV in ("bae", ): from bae.api import bcs BAE_BCS = bcs.BaeBCS(const.BCS_ADDR, const.ACCESS_KEY, const.SECRET_KEY) BCS_HOST = "http://bcs.duapp.com" BUCKET_NAME = "deepgully" BSC_FOLDER = "/photos/" elif RUNTIME_ENV in ("local", ): UPLOAD_URL = "static/uploads/" UPLOAD_FOLDER = os.path.join(app.root_path, UPLOAD_URL) elif RUNTIME_ENV in ("gae", "gae_dev"): BLOB_SERVING_URL = "/_files" app.config["BLOB_SERVING_URL"] = BLOB_SERVING_URL THUMB_SIZE = (400, 300)
def enter_class(request, classid): content = {'validate':True, 'profile':False, 'classid':classid} quest = {} stu = {} HOST = const.BCS_ADDR AK = const.ACCESS_KEY SK = const.SECRET_KEY bname = 'mediaavatar' baebcs = bcs.BaeBCS(HOST, AK, SK) e, d = baebcs.list_objects(bname) imgids = [] d = str(d).replace('\'', '\"') d = json.loads(d) for i in d: try: imgid = int(i.replace('.jpg','').replace('/','')) imgids.append(imgid) except:pass if str(classid) == str(request.user): content.update({'validate':False, 'profile':True}) for i in Student.objects.filter(classs=Class(classid=classid)): try: pt = re.compile(r'\(\d+\.\d+,\d+\.\d+\)') s = pt.findall(i.position)[-1].replace('(','').replace(')','') lng = s.split(',')[0] lat = s.split(',')[1] addr = i.position.replace('('+lng+','+lat+')', '') except: addr = '暂无' lng = '10.0' lat = '10.0' if i.birthday == '':birth = '暂无' elif i.birthday[0] == 'g':birth = i.birthday[1:]+'(公历)' else:birth = i.birthday[1:]+'(农历)' if i.name == '': i.name = '暂无' if i.qq == '': i.qq = '暂无' if i.weibo == '': i.weibo = '暂无' if i.phone == '': i.phone = '暂无' if i.mail == '': i.mail = '暂无' if i.weixin == '': i.weixin = '暂无' if i.studentid in imgids: imgid = str(i.studentid) else:imgid = 'error' stu.update({str(i.studentid):{'studentnum':i.studentnum,\ 'name':i.name, 'qq':i.qq,\ 'weibo':i.weibo, 'phone':i.phone, 'mail':i.mail,\ 'birth':birth, 'addr':addr, 'lng':lng, 'lat':lat, 'imgid':imgid, 'weixin':weixin}}) elif request.method == 'POST': form = ValidateForm(request.POST) if form.is_valid(): answer1 = form.cleaned_data['answer1'] answer2 = form.cleaned_data['answer2'] answer3 = form.cleaned_data['answer3'] c = [i for i in Class.objects.filter(classid=classid)][0] if answer1.encode('utf8') in c.answer1.split('|') and\ answer2.encode('utf8') in c.answer2.split('|') and\ answer3.encode('utf8') in c.answer3.split('|'): content.update({'validate':False, 'profile':True}) for i in Student.objects.filter(classs=Class(classid=classid)): try: pt = re.compile(r'\(\d+\.\d+,\d+\.\d+\)') s = pt.findall(i.position)[-1].replace('(','').replace(')','') lng = s.split(',')[0] lat = s.split(',')[1] addr = i.position.replace('('+lng+','+lat+')', '') except: addr = '暂无' lng = '10.0' lat = '10.0' if i.birthday == '':birth = '暂无' elif i.birthday[0] == 'g':birth = i.birthday[1:]+'(公历)' else:birth = i.birthday[1:]+'(农历)' if i.name == '': i.name = '暂无' if i.qq == '': i.qq = '暂无' if i.weibo == '': i.weibo = '暂无' if i.phone == '': i.phone = '暂无' if i.mail == '': i.mail = '暂无' if i.studentid in imgids: imgid = str(i.studentid) else:imgid = 'error' stu.update({str(i.studentid):{'studentnum':i.studentnum,\ 'name':i.name, 'qq':i.qq,\ 'weibo':i.weibo, 'phone':i.phone, 'mail':i.mail,\ 'birth':birth, 'addr':addr, 'lng':lng, 'lat':lat, 'imgid':imgid, 'weixin':weixin}}) user = authenticate(username=str(classid)) login(request, user) c = [i for i in Class.objects.filter(classid=classid)][0] quest.update({'answer1':[c.quest1,'']}) quest.update({'answer2':[c.quest2,'']}) quest.update({'answer3':[c.quest3,'']}) return render_to_response(sys._getframe().f_code.co_name + '.html', locals(), context_instance=RequestContext(request))
def save_to_bcs(self, path, body): from bae.core import const from bae.api import bcs import base64 baebcs = bcs.BaeBCS(const.BCS_ADDR, const.ACCESS_KEY, const.SECRET_KEY) baebcs.put_object(u'fashionramlab', path, base64.b64encode(body))
def get_from_bcs(self, path): from bae.core import const from bae.api import bcs baebcs = bcs.BaeBCS(const.BCS_ADDR, const.ACCESS_KEY, const.SECRET_KEY) e, response = baebcs.get_object(u'fashionramlab', path) return base64.b64decode(response)