发送“304未修改”存储在数据存储区中的图像 [英] Send a "304 Not Modified" for images stored in the datastore
问题描述
如 db.Blob 存储在Google App Engine数据存储区中/appengine/docs/python/images/usingimages.htmlrel =nofollow noreferrer>文档。然后我在 /images/<id>.jpg
中提供这些图片。
服务器总是发送一个 200 OK
响应,这意味着浏览器必须多次下载相同的图像(==较慢),并且服务器必须多次发送相同的图像(==更多昂贵的)。
由于大多数图片可能永远不会改变,我希望能够发送 304 Not Modified
回应。我正在考虑在用户上传图片时计算某种图片的哈希值,然后使用它来了解用户是否已经拥有此图片(可以将哈希值作为 Etag
?)
我发现这个答案和这个答案,这个答案很好地解释了逻辑,但我有两个问题:
- 是否可以在Google App Engine中发送
Etag
? - 是否有人实现了这样的逻辑,和/或是否有任何代码片段可用? Bloggart 使用这种技术。查看这篇博文。
- Is it possible to send an
Etag
in Google App Engine? - Has anyone implemented such logic, and/or is there any code snippet available?
$ p $
class StaticContentHandler(webapp.RequestHandler):
def output_content(self,content,serve = True) :
self.response.headers ['Content-Type'] = content.content_type
last_modified = content.last_modified.strftime(HTTP_DATE_FMT)
self.response.headers ['Last-Modified' ] = last_modified
self.response.headers ['ETag'] =''%s''%(content.etag,)
如果服务:
self.response.out.write( content.body)
else:
self.response.set_status(304)
def get(self,path):
content = get(path)
如果不是内容:
self.error(404)
返回
serve = True
if'If-Modified-Since'in self.request.headers :
last_seen = datetime.datetime.strptime(
self.request.headers ['If-Modi fied-Since'],
HTTP_DATE_FMT)
如果last_seen> = content.last_modified.replace(微秒= 0):
serve = False
if'None-Match 'in self.request.headers:
etags = [x.strip(''')
for self.request.headers ['If-None-Match']。split(',' )]
if etags中的content.etag:
serve = False
self.output_content(content,serve)
I store user-uploaded images in the Google App Engine datastore as db.Blob
, as proposed in the docs. I then serve those images on /images/<id>.jpg
.
The server always sends a 200 OK
response, which means that the browser has to download the same image multiple time (== slower) and that the server has to send the same image multiple times (== more expensive).
As most of those images will likely never change, I'd like to be able to send a 304 Not Modified
response. I am thinking about calculating some kind of hash of the picture when the user uploads it, and then use this to know if the user already has this image (maybe send the hash as an Etag
?)
I have found this answer and this answer that explain the logic pretty well, but I have 2 questions:
Bloggart uses this technique. Have a look at this blog post.
class StaticContentHandler(webapp.RequestHandler):
def output_content(self, content, serve=True):
self.response.headers['Content-Type'] = content.content_type
last_modified = content.last_modified.strftime(HTTP_DATE_FMT)
self.response.headers['Last-Modified'] = last_modified
self.response.headers['ETag'] = '"%s"' % (content.etag,)
if serve:
self.response.out.write(content.body)
else:
self.response.set_status(304)
def get(self, path):
content = get(path)
if not content:
self.error(404)
return
serve = True
if 'If-Modified-Since' in self.request.headers:
last_seen = datetime.datetime.strptime(
self.request.headers['If-Modified-Since'],
HTTP_DATE_FMT)
if last_seen >= content.last_modified.replace(microsecond=0):
serve = False
if 'If-None-Match' in self.request.headers:
etags = [x.strip('" ')
for x in self.request.headers['If-None-Match'].split(',')]
if content.etag in etags:
serve = False
self.output_content(content, serve)
这篇关于发送“304未修改”存储在数据存储区中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!