0

I built a python lambda which generates a zip file containing a certificate and password. The file is base64 encoded and returned using proxy integration through the API gateway. Hitting the API gateway with a the browser prompts to download. The response is saved to a file with a zip extension.

The saved file is still base64 encoded. The end users should receive a working zip file. Base64 decoding the file gives a valid zip file. The user should not have to decode the file.

Adding a Content-Encoding header causes the API gateway integration to fail with the error that it cannot base64 decode the response from the lambda.

Note - Stripping the leading and trailing characters from the b64content fixes the str() function putting a leading b' and trailing tic. Can you tell I'm a noob to python and AWS? ;-)

                headers = {'Content-Type': 'application/zip,  application/octet-stream',
                           'Content-Disposition': f'attachment; filename="{filename}.zip"'
                           }
                if 'b64' in event['params']:
                    headers['Content-Encoding'] = 'base64'

                response = {
                    'statusCode': 200,
                    'isBase64Encoded': True,
                    'headers': headers,
                    'body': str(b64content)[2:-1],
                }
CatManDo
  • 11
  • 3

1 Answers1

1

The answer was due to 2 different issues.

First, the AWS sample code is Python2. The lambda is Python3. The encoding for strings and bytes has changed in Python3 so the base64 byte array needed .decode('utf-8') before returning it. (The encoding is not required but explicit is better than implicit)

Second, the API gateway was not properly treating the returned data as base64 encoded. Adding the content-encoding: base64 header caused the API to decode the base64 data before returning it to the client.

This question about a runtime marshaling error got me half of the answer.

CatManDo
  • 11
  • 3