logandk / serverless-wsgi

Serverless plugin to deploy WSGI applications (Flask/Django/Pyramid etc.) and bundle Python packages
MIT License
494 stars 88 forks source link

[Question] Binary data with send_file #192

Closed bluthen closed 3 years ago

bluthen commented 3 years ago

I was looking to send some generated binary data that needs to support byte-range requests. The following works fine in normal flask:

ret_response = make_response(send_file(io.BytesIO(response),
            max_age=CACHE_AGE,
            mimetype='audio/mpeg'))
ret_response.headers['myspecialheader'] = 'somevalue'
return ret_response

However in serverless-wsgi I get base64 encoded data from lambda instead of binary data I am expecting. I think the issue is serverless-wsgi is not setting the isBase64Encoded=true value when returning back from lambda. Is there a way this can be forced, maybe a header that is set. Or maybe it is an issue with the detection?

bluthen commented 3 years ago

I put in some debug messages in serverless-wsgi.py and I see isBase64Encoded being set to true, so not sure what is happening.

bluthen commented 3 years ago

I was too unfamiliar with lambda and binary encoding. In case anyone else gets this issue I needed to:

Adding audio/mpeg specifically was also not enough because I couldn't control what the browser sets in Accept header in this case.

You can do this also with a severless plugin: https://github.com/maciejtreder/serverless-apigw-binary