2013-07-03 3 views
3

텍스트와 이미지를 업로드하는 앱을 만들고 있습니다. Blobstore와 Google High Performance Image Serving에 대해 많은 것을 읽었으며 마침내이를 모두 구현할 수있는 방법을 얻었습니다.GAE + NDB + Blobstore + Google 고성능 이미지 검색

내가 알고 싶은 것은 모든 것이 잘 수행되었거나 더 나은 방법으로 수행 될 수 있으며 모델의 serving_url을 저장하는 것이 더 나은 경우 또는 이미지를 인쇄 할 때마다 계산해야한다는 것입니다. 그 페이지.

사용자와 그림 만 있습니다. 나는 기본적으로

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) 

:

은 (단지 쉽게 페이지를 렌더링하는 기능을 가지고, 내 custom.PageHandler 잊어 요약 및 체크 형태의 값에 대한 물건 등) 코드입니다 이 선이있는 jinja2 템플릿에 이미지를 보여주는 모든 사진을 목록 :
{% for line in data %} 
    <tr> 
    <td class="col-center-data"><img src="{{ line.servingUrl }}"></td> 

그래서 List 클래스에서 나는 각각의 역할 URL을 계산하고 모델에 일시적으로 추가 할 수 있습니다. 나는 URL이 시간에 따라 변할지 모른다는 이유로 모델에 직접 저장하는 것이 좋은지 정확히 알지 못합니다. 이미지의 URL은 영구적입니까? 이 경우 계산 대신 사실을 저장할 수 있습니까?

Send 클래스는 이미지를 업로드 할 양식 만 표시하고 데이터를 모델에 저장합니다. docs가 그것에 대해 이야기하기 때문에 항상 페이지를 다시 렌더링하는 경우 새로운 form_action 링크를 생성합니다. 맞아?

코드가 작동하지만 성능 및 자원 절약 측면에서 어느 것이 더 좋은 방법인지 알고 싶습니다.

답변

4

네 말이 맞아. 반복적으로 호출하는 대신 get_serving_url()을 저장하려고합니다. 그것은 동일하게 유지됩니다.

blob을 삭제할 때처럼 URL을 완료하면 delete_serving_url()이 있다는 것을 유의하십시오.

+0

확인해 주셔서 감사합니다. @dragonx – Eagle