【问题标题】:GAE + NDB + Blobstore + Google High Performance Image ServingGAE + NDB + Blobstore + Google 高性能图像服务
【发布时间】:2013-07-01 07:33:23
【问题描述】:

我正在制作一个用于上传文本和图像的应用。我已经阅读了很多关于 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 类中,我计算每个服务 url 并将其临时添加到模型中。我不知道直接将它保存在模型中是否会好,因为我不知道url是否会随着时间而改变。将是图像的永久网址吗?在那种情况下,我可以保存它而不是计算,对吗?

Send 类只显示一个表单来上传图片并将数据保存到 Model。在重新渲染页面的情况下,我总是会生成一个新的 form_action 链接,因为文档会讨论它。对吗?

代码正在运行,但我想知道哪种方法更好,就性能和资源节省而言。

【问题讨论】:

    标签: python image google-app-engine blobstore app-engine-ndb


    【解决方案1】:

    你是对的。您确实想保存 get_serving_url() 而不是重复调用它。它保持不变。

    请注意,当您完成 url 时,会有一个 delete_serving_url(),就像您删除 blob 时一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多