问题描述
我想从Chalice/python应用程序返回图像.我的整个应用程序代码粘贴在下面:
from chalice import Chalice, Response
import base64
app = Chalice(app_name='hello')
@app.route('/makeImage', methods=['GET'])
def makeImage():
return Response(
base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
),
headers={
'Content-Type': 'image/jpeg'
},
status_code=200)
结果...
为什么会这样?
我已经阅读了大量文档,并且由于最近对Chalice添加了二进制支持,因此大多数文档已经过时了:
- https://github.com/aws/chalice/pull/352
- https://github.com/aws/chalice/issues/592
- https://github.com/aws/chalice/issues/348
- AWS Chalice从S3返回图像文件(警告:这个问题的唯一答案是完全错误)
- https://chalice.readthedocs.io/en/latest/api.html
- https://github.com/aws/chalice/issues/391 (问题在2017年彻底关闭,没有解决方案)
- https://github.com/aws/chalice/issues/1095 重新开放了391以上的位置
仅出于故障排除的目的,我可以使用curl -H "accept: image/jpeg"
来获取响应,但这是没有用的,因为浏览器无法以这种方式工作,并且我需要在浏览器中使用响应(HTML IMG TAG)./p>
更新
我也尝试过@app.route('/makeImage', methods=['GET'], content_types=['image/jpeg'])
结果变为{"Code":"UnsupportedMediaType","Message":"Unsupported media type: application/json"}
我遇到了同样的问题.
如果不存在标头接受,AWS将其设置为默认application/json,我将收到base64响应.如果将header设置为images/jpeg或任何二进制内容类型,那么我得到了图像.很棒,但是网络浏览器没有设置accept标头.
但是,如果我添加
app.api.binary_types =['*/*']
然后确定,我的图像API现在可以使用了. 太好了,但是现在json失败了.
目前,除了有两个API网关外,我看不到任何解决方案:一个用于json,一个用于图像.如果您真的只想要一个API网关,我认为您必须在所有json响应上使用gzip压缩将它们转换为二进制文件.
AWS API网关如何与lambda代理一起使用,而不是圣杯问题.但我同意,这是一个很大的限制
I want to return an image from a Chalice/python application. My entire application code is pasted below:
from chalice import Chalice, Response
import base64
app = Chalice(app_name='hello')
@app.route('/makeImage', methods=['GET'])
def makeImage():
return Response(
base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
),
headers={
'Content-Type': 'image/jpeg'
},
status_code=200)
The result...
Why does this happen?
I have poured through a ton of documentation already and most of it is outdated as binary support was added to Chalice very recently:
- https://github.com/aws/chalice/pull/352
- https://github.com/aws/chalice/issues/592
- https://github.com/aws/chalice/issues/348
- AWS Chalice Return an Image File from S3 (Warning: the sole answer to this question is COMPLETELY WRONG)
- https://chalice.readthedocs.io/en/latest/api.html
- https://github.com/aws/chalice/issues/391 (issue WRONGLY CLOSED in 2017 without a resolution)
- https://github.com/aws/chalice/issues/1095 is a re-open of 391 above
Just for troubleshooting purposes I'm able to obtain a response by using curl -H "accept: image/jpeg"
, but this is useless since browsers to not work this way, and I need to use the response in a browser (HTML IMG TAG).
UPDATE
I also tried @app.route('/makeImage', methods=['GET'], content_types=['image/jpeg'])
And result became {"Code":"UnsupportedMediaType","Message":"Unsupported media type: application/json"}
I had the same issue.
If no header accept is present, AWS set it to default application/json and I receive a base64 response. If I set accept to images/jpeg or any binary content type in header, then I got the images. Great but web browser to not set the accept header.
But if I add
app.api.binary_types =['*/*']
then ok my images apis now works. Great but now the json ones fail.
Currently I do not see any solution except having two API gateway : one for json and one for images. If you really want only one API Gateway, I think you have to use gzip conpression on all you json response to convert them to binaries.
It is more how AWS API Gateway works with lambda proxy than a Chalice issue. But I agree, it is a big limitation
这篇关于Chalice Framework:请求未指定带有image/jpeg的Accept标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!