我目前正在存储给定产品的上传图片,如下所示:
class Product(db.Model):
images= db.ListProperty(db.Blob)
# More properties ...
我用以下方法检索它们:
class ImageHandler(webapp2.RequestHandler):
def get(self):
productKey = cgi.escape(self.request.get('id'))
if productKey:
try:
product = Product.get(db.Key(productKey))
if product and product.images:
idx = 0 # if not img is passed, return the first one
imageIndex = cgi.escape(self.request.get('img'))
if imageIndex:
imageIndex = int(imageIndex)
if imageIndex > 0 and imageIndex < len(product.images):
idx = imageIndex
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(str(product.images[idx]))
return
self.redirect('/img/image-missing.jpg')
return
except:
self.redirect('/img/image-404.jpg')
return
self.redirect('/img/image-404.jpg')
通过调用
/image?id={{productkey}}&img={{imageindex}}
。现在考虑:
数据存储成本
CPU /内存/时间成本
可能迁移到另一个应用程序/数据存储,甚至迁移到亚马逊等。
能够返回调整大小的图像以显示缩略图等。
我没有考虑的其他因素
我应该像这样保留它,还是应该有一个单独的图像模型,该模型仅包含
db.BlobProperty
,而在产品模型中却具有db.ListProperty(db.Key)
?我不确定将图像保存为产品中的斑点是否会在get()产品时自动获取(这很不好),或者是否减少了数据存储访问量,因为我不必获取其他图像实体。
最后,任何改进此python代码的建议(很多重定向,返回和条件)也将不胜感激。
谢谢。
最佳答案
如果您认为每个数据存储实体都绑定到1MB大小,那么将Blob列表存储到一个实体中很容易成为问题。最好将每个图像存储在单独的模型中,并保留产品模型的密钥列表。