【发布时间】:2018-10-07 02:55:28
【问题描述】:
在我基于 Flask 的 Google App Engine 服务器中,我试图返回一个带有“content-length”标头的响应,该标头将包含提供给客户端的 blob 的最终大小。该 blob 是一个大型媒体文件,因此该标头将用于设置 UI 前端进度条的最大值。 Blob 位于 Cloud Storage 中,但使用 App Engine 包中的 blobstore API 来检索 Blob。下面返回一个200 状态码:
response.headers['Content-length'] = blobstore.BlobInfo(version.blob_key).size
return response
它正在使用Requests Python package 接收,并且正在流式传输正在接收的数据 (This should be fine, since all the headers are received in the Response object first before data streaming begins)。我的代码如下所示:
with requests.get(url='/api/download', stream=True) as r:
print(r.headers)
但是,没有设置 Content-length 标头。相反,我从上面的print() 得到类似的东西:
{'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'}
我从this post 注意到,App Engine 去除了 content-length 标头并将其替换为正确的大小,但我一开始没有得到 content-length 标头。我做错了什么?我是否将 blob 提供给客户错误?内容长度是否根本不可用且无法设置?如果是这样,我如何检索文件的大小,以便计算我的 UI 进度条的大小?
【问题讨论】:
标签: python google-app-engine python-requests google-cloud-storage blobstore