![](/img/trans.png)
[英]Google App Engine django model form does not pick up BlobProperty
[英]Google App Engine BlobProperty returns stale content
我將縮略圖圖像作為BlobStoreProperties存儲在Google App引擎實體中。 隨着時間的流逝,縮略圖需要更新,這是通過使用新鮮的圖像數據更新這些實體的內容來完成的。 但是,我發現,這些圖像的任何后續檢索仍然會返回與實體中首次保存的相同的舊副本。 這是令人驚訝的不一致行為。 我編寫了一個簡單的獨立代碼來驗證這一點。
這是兩個簡單的處理程序和一個模型定義。 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 "imgdata=@/tmp/img1.png" http://ubuntu.local:8000/_thumbsave/X
我檢索圖像
curl -d dummy=0 http://ubuntu.local:8000/_thumbload/X > Downloads/imgout.png
imgout.png
和img1.png
相同
然后我上傳了另一張圖片img2.png
curl -F "imgdata=@/tmp/img2.png" http://ubuntu.local:8000/_thumbsave/X
然后以上面相同的方式檢索它。 我希望現在imgout.png
與img2.png
相同。 但是相反,我發現它仍然是舊的img1.png。 因此,Image查詢返回了陳舊的對象。 打印圖像長度的日志語句還驗證第二次返回的圖像不是更新的圖像。
這里出了什么問題?
在SaveImageHandler
,每次發布圖像數據時都將創建一個新的Image
實體,然后您將在LoadImageHandler
獲取具有該uid的第一個圖像
將其更改為“查找或創建”圖像,例如:
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方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.