python-GAE NDB Blobstore Google高性能图片服务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-GAE NDB Blobstore Google高性能图片服务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3089字,纯文字阅读大概需要5分钟。
内容图文
![python-GAE NDB Blobstore Google高性能图片服务](/upload/InfoBanner/zyjiaocheng/677/9b43c0bcf6014411863785d052d70736.jpg)
我正在制作一个用于上传文本和图像的应用程序.我已经阅读了很多有关blobstore和Google高性能图片服务的文章,最后我找到了一种将它们全部实现的方法.
我想知道的是,是否一切都做得好或可以做得更好,以及将serving_url保存在模型中还是更好,还是必须在每次我要在页面中打印图像时进行计算.
只有一个用户和一个图片.
这是代码(概述,忘记我的custom.PageHandler,它仅具有轻松呈现页面的功能以及用于检查表单值的内容,等等):
class User(ndb.Model):
""" A User """
username = ndb.StringProperty(required=True)
password = ndb.StringProperty(required=True)
email = ndb.StringProperty(required=True)
class Picture(ndb.Model):
""" All pictures that a User has uploaded """
title = ndb.StringProperty(required=True)
description = ndb.StringProperty(required=True)
blobKey = ndb.BlobKeyProperty(required=True)
servingUrl = ndb.StringProperty()
created = ndb.DateTimeProperty(auto_now_add=True)
user = ndb.KeyProperty(kind=User)
# This class shows the user pics
class List(custom.PageHandler):
def get(self):
# Get the actual user pics
pics = Picture.by_user(self.user.key)
for pic in pics:
pic.servingUrl = images.get_serving_url(pic.blobKey, size=90, crop=True)
self.render_page("myPictures.htm", data=pics)
# Get and post for the send page
class Send(custom.PageHandler, blobstore_handlers.BlobstoreUploadHandler):
def get(self):
uploadUrl = blobstore.create_upload_url('/addPic')
self.render_page("addPicture.htm", form_action=uploadUrl)
def post(self):
# Create a dictionary with the values, we will need in case of error
templateValues = self.template_from_request()
# Test if all data form is valid
testErrors = check_fields(self)
if testErrors[0]:
# No errors, save the object
try:
# Get the file and upload it
uploadFiles = self.get_uploads('picture')
# Get the key returned from blobstore, for the first element
blobInfo = uploadFiles[0]
# Add the key to the template
templateValues['blobKey'] = blobInfo.key()
# Save all
pic = Picture.save(self.user.key, **templateValues)
if pic is None:
logging.error('Picture save error.')
self.redirect("/myPics")
except:
self.render_page("customMessage.htm", custom_msg=_("Problems while uploading the picture."))
else:
# Errors, render the page again, with the values, and showing the errors
templateValues = custom.prepare_errors(templateValues, testErrors[1])
# The session for upload a file must be new every reload page
templateValues['form_action'] = blobstore.create_upload_url('/addPic')
self.render_page("addPicture.htm", **templateValues)
基本上,我列出所有图片,并在此行的jinja2模板中显示图像:
{% for line in data %}
<tr>
<td class="col-center-data"><img src="{{ line.servingUrl }}"></td>
因此,在List类中,我计算每个服务网址并将其临时添加到Model中.我不确切知道将其直接保存在模型中是否会很好,因为我不知道URL是否会随时间变化.图片的网址将是永久的吗?在那种情况下,我可以保存它而不是计算,是吗?
Send类仅显示用于上载图像的表单,并将数据保存到模型.在重新渲染页面的情况下,我总是生成一个新的form_action链接,因为文档对此进行了讨论.这样对吗?
该代码正在运行,但就性能和节省资源而言,我想知道哪种方法更好.
解决方法:
你是对的.您确实想保存get_serving_url()而不是重复调用它.它保持不变.
请注意,使用完网址后,就像删除Blob一样,有一个delete_serving_url().
内容总结
以上是互联网集市为您收集整理的python-GAE NDB Blobstore Google高性能图片服务全部内容,希望文章能够帮你解决python-GAE NDB Blobstore Google高性能图片服务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。