Binary requests and responses
AWS Lambda is only used for executing code. Serving assets via PHP does not make sense as this would be a waste of resources and money.
In some cases however, you want to serve images (or other assets) via PHP. One example would be if you served generated images or PDFs via PHP.
By default, API Gateway does not support binary HTTP requests or responses like images, PDF, binary files… To achieve this, you need to enable the option for binary media types in
serverless.yml as well as define the
BREF_BINARY_RESPONSES environment variable:
provider: # ... apiGateway: binaryMediaTypes: - '*/*' environment: BREF_BINARY_RESPONSES: '1'
This will make API Gateway support binary file uploads and downloads, and Bref will automatically encode responses to base64 (which is what API Gateway expects for binary responses).
Be aware that the max upload and download size is 6MB. For larger files, use AWS S3. An example is available in Serverless Visually Explained (opens in a new tab).