未在Flask App Engine响应上为投放的Blob设置内容长度标头 [英] Content-length header not being set on Flask App Engine response for served blob

查看:102
本文介绍了未在Flask App Engine响应上为投放的Blob设置内容长度标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于Flask的Google App Engine服务器中,我尝试返回带有"content-length"标头的响应,该标头包含要提供给客户端的Blob的最终大小.这个Blob是一个大型媒体文件,因此该标头将用于设置UI前端上进度条的最大值.该Blob位于Cloud Storage中,但是正在使用App Engine软件包中的Blobstore API来检索Blob.以下返回的状态码为 200 :

In my Flask-based Google App Engine server, I am trying to return a response with a 'content-length' header which will contain the final size of a blob being served to the client. This blob is a large media file, so this header is going to be used to set the maximum value of a progress bar on the UI frontend. The blob lives in Cloud Storage, but is using the blobstore API from the App Engine packages to retrieve the blob. Below returns with a 200 status code:

response.headers['Content-length'] = blobstore.BlobInfo(version.blob_key).size
return response

正在使用请求Python包接收它,并且正在流式传输接收到的数据(应该没问题,因为所有则在数据流开始传输之前先在 Response 对象中接收标头).我的代码如下所示:

It is being received using the Requests Python package, and is streaming the data being received (This should be fine, since all the headers are received in the Response object first before data streaming begins). My code looks something like below:

with requests.get(url='/api/download', stream=True) as r:
    print(r.headers)

但是,没有设置 Content-length 标头.相反,我从上面的 print()中得到了类似的东西:

However, there is no Content-length header being set. Instead, I get something like this from the print() above:

{'Cache-Control': 'no-cache', 
 'Content-Type': 'text/plain; charset=utf-8', 
 'X-Cloud-Trace-Context': '1bca54f9cdae7394de6cb2f1e824ba6f', 
 'Transfer-Encoding': 'chunked',
 'Date': 'Thu, 26 Apr 2018 11:36:17 GMT',
 'Server': 'Google Frontend'}

我从这篇文章中注意到,App Engine剥离了内容-length标头,并将其替换为正确的大小,但是我没有开始使用content-length标头.我做错了什么?我向客户提供Blob服务是否错误?内容长度是否根本不可用并且无法设置?如果是这样,如何获取文件的大小,以便可以计算UI进度栏的大小?

I noticed from this post that App Engine strips the content-length header and replaces it with the correct size, but I'm not getting a content-length header to begin with. What am I doing incorrectly? Am I serving the blob to the client wrong? Is the content-length simply unavailable and cannot be set? If so, how can I retrieve the size of the file so I can compute the size of my UI progress bar?

推荐答案

Content-Length 标头包含在正如您和文档所指出的,App Engine从请求数据中计算Content-Length标头,然后将其添加到请求中,然后再发送.

As you and the documentation pointed out, App Engine calculates the Content-Length header from the request data and adds it to the request prior to sending.

您始终可以设置个性化标题,Flask允许,而GAE则允许您使用它.

You can always set a personalized header, Flask allows it, and GAE will let you use it.

例如,对于您的答复:

response.headers['CL-blob'] = blobstore.BlobInfo(version.blob_key).size

这篇关于未在Flask App Engine响应上为投放的Blob设置内容长度标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆