2011-06-19 66 views
1

我將縮略圖圖像作爲BlobStoreProperties存儲在Google App引擎實體中。隨着時間的推移,縮略圖需要更新,我通過用新的圖像數據更新這些實體的內容來完成更新。但是,我發現任何後續檢索這些圖像仍會返回實體中第一次保存的相同舊副本。這是令人驚訝的不一致的行爲。我寫了一個簡單的獨立代碼來驗證這一點。Google App Engine BlobProperty返回陳舊內容

這裏有兩個簡單的處理程序和一個模型定義。 SaveImageHandler將圖像保存到數據存儲區,LoadImageHandler檢索它。

from google.appengine.ext import db 
import logging 

class Image(db.Expando): 
data = db.BlobProperty(required=True) 
uid = db.StringProperty(required=True) 

class SaveImageHandler(webapp.RequestHandler):         
    def post(self, uid):               
    imgdata = self.request.POST.get('imgdata').file.read()      
    logging.error('Saving %d bytes'%(len(imgdata)))       
    image = model.Image(data=imgdata, uid=uid)         
    image.put()                

class LoadImageHandler(webapp.RequestHandler):         
    def post(self, uid):               
    image = model.Image.gql('WHERE uid = :1', uid).get()      
    self.response.headers['Content-type'] = 'image/png'      
    logging.error('Loading %d bytes'%(len(image.data)))      
    self.response.out.write(image.data)  

def application(): 
    return webapp.WSGIApplication([ 
    ('/_thumbsave/(.*)', SaveImageHandler), 
    ('/_thumbload/(.*)', LoadImageHandler), 
    ],debug=False) 

def main(): 
    util.run_wsgi_app(application()) 

if __name__ == '__main__': 
    main() 

我上傳的圖像這樣

curl -F "[email protected]/tmp/img1.png" http://ubuntu.local:8000/_thumbsave/X 

我檢索圖像

curl -d dummy=0 http://ubuntu.local:8000/_thumbload/X > Downloads/imgout.png 

imgout.pngimg1.png相同

然後我上傳其他圖片img2.png

curl -F "[email protected]/tmp/img2.png" http://ubuntu.local:8000/_thumbsave/X 

然後以上面的相同方式檢索它。我期望現在imgout.pngimg2.png相同。但是,我發現它仍然是舊的img1.png。因此Image查詢返回了陳舊的對象。打印圖像長度的日誌語句也會驗證第二次返回的圖像不是更新後的圖像。

這裏怎麼回事?

回答

3

在你SaveImageHandler要創建一個新的Image實體每次您發佈的圖像數據的時間,那麼你只是與UID獲取的第一圖像在LoadImageHandler

將其更改爲「尋找或創造」像這樣的:

class SaveImageHandler(webapp.RequestHandler): 
    def post(self, uid): 
    image = Image.all().filter("uid =", uid).get() 
    if not image: 
     image = model.Image(uid=uid) 
    image.data = self.request.POST.get('imgdata').file.read() 
    image.put() 

不是使用uid性質,考慮使用key_names爲目的,並採取看看get_or_insert方法

+0

哇。非常尷尬現在忽略這一點。感謝您指出。 – Jayesh

+0

鍵名肯定是在這裏做的正確的事情 - 他們會避免這個問題,並減少開銷的開銷。 –