gpt4 book ai didi

python - Google App Engine BlobProperty 返回过时的内容

转载 作者:行者123 更新时间:2023-11-30 23:52:15 24 4
gpt4 key购买 nike

我将缩略图存储在 Google App 引擎实体中作为 BlobStoreProperties。随着时间的推移,缩略图需要更新,我通过使用新的图像数据更新这些实体的内容来完成此操作。然而,我发现对这些图像的任何后续检索仍然返回第一次在实体中保存的相同旧副本。这是令人惊讶的不一致行为。我编写了一个简单的独立代码来验证这一点。

这里有两个简单的处理程序和一个模型定义。 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.pngimg1.png 相同

然后我上传另一张图片img2.png

curl -F "imgdata=@/tmp/img2.png" http://ubuntu.local:8000/_thumbsave/X

然后用上面同样的方式检索它。我现在希望 imgout.pngimg2.png 相同。但我发现它仍然是旧的 img1.png。因此,图像查询返回陈旧的对象。打印图像长度的日志语句,也验证了第二次返回的图像不是更新的图像。

这里出了什么问题?

最佳答案

在您的 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()

考虑使用 key_names 来实现此目的,而不是使用 uid 属性,并查看 get_or_insert方法

关于python - Google App Engine BlobProperty 返回过时的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com