首页 > 解决方案 > 如何提供来自 Google App Engine 的 GZIP 编码图像

问题描述

我正在尝试获取链接共享以包含 Facebook 上的图片。Facebook 共享调试器表示图像没有使用 gzip 编码,这显然是一项要求。

使用 curl,我可以确认 Facebook 在说什么。我能找到的所有文档都说 gzip 编码应该自动发生,所以我不确定如何解决这个问题。

这是卷曲输出:

$ curl -v -D /tmp/h -H "Accept-Encoding: gzip" http://m.kaon.com/ss/5907657076506624.jpg > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 172.217.3.115...
* TCP_NODELAY set
* Connected to m.kaon.com (172.217.3.115) port 80 (#0)
> GET /ss/5907657076506624.jpg HTTP/1.1
> Host: m.kaon.com
> User-Agent: curl/7.54.0
> Accept: */*
> Accept-Encoding: gzip
> 
< HTTP/1.1 200 OK
< Cache-Control: no-cache
< Content-Type: image/jpeg
< X-Cloud-Trace-Context: 5b0c467df46cb34732e5721733742742
< Date: Wed, 13 Mar 2019 15:11:08 GMT
< Server: Google Frontend
< Content-Length: 128384
< 
{ [3516 bytes data]
100  125k  100  125k    0     0   620k      0 --:--:-- --:--:-- --:--:--  623k
* Connection #0 to host m.kaon.com left intact
$ cat /tmp/h
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: image/jpeg
X-Cloud-Trace-Context: 5b0c467df46cb34732e5721733742742
Date: Wed, 13 Mar 2019 15:11:08 GMT
Server: Google Frontend
Content-Length: 128384

这是提供图像的代码:

class SSHandler(webapp2.RequestHandler):
  def get(self, id):
    a = ScreenShotModel.get_by_id(int(id))
    self.response.headers['Content-Type'] = 'image/jpeg'
    self.response.out.write(a.image)

是否缺少一些 app.yaml 设置,或者我需要在标题中添加其他内容?或者我应该自己寻找接受编码标头并在我的 python 代码中执行 gzip?

标签: pythonhttpgoogle-app-enginewebapp2

解决方案


对 jpeg 进行 gzip 压缩是没有意义的。它已经被压缩了。

有一组允许的 mime 类型用于压缩。JPEG 不是其中之一。

白名单中典型的有:

文本/纯文本、文本/html、应用程序/javascript等


推荐阅读