【问题标题】:How can I serve GZIP-encoded images from Google App Engine如何提供来自 Google App Engine 的 GZIP 编码图像
【发布时间】:2019-08-04 08:09:07
【问题描述】:

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

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

这是 curl 输出:

$ 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?

【问题讨论】:

  • 您是否尝试在您的代码中添加self.response.headers['Content-Encoding'] = 'gzip',以在标头中添加编码类型?
  • 如果它有效,那将是非常令人惊讶的。无论如何,我试过了,它并没有改变任何东西。事实上,GAE 将其剥离。 GAE 不允许您设置内容编码标头。 cloud.google.com/appengine/docs/standard/go/reference/…
  • Facebook 自发开始接受我的非 gzip 压缩图像,所以我想我不需要回答这个问题。但我还是很好奇,如果有人知道答案的话。

标签: python http google-app-engine webapp2


【解决方案1】:

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

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

白名单中典型的有:

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

【讨论】:

  • JPEG 是逐块压缩的,事实上,JPEG 的 GZIP 通常会生成更小的文件,因为块之间存在相似性。想要对 JPEG 进行 GZIP 压缩是完全有效的,但奇怪的是 GAE 似乎不允许这样做。
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
相关资源
最近更新 更多