Skip to content

Instantly share code, notes, and snippets.

@janrubio
Last active November 20, 2020 21:35
Show Gist options
  • Save janrubio/c6d2adf55e4bb4ef262f7e92c46de2f4 to your computer and use it in GitHub Desktop.
Save janrubio/c6d2adf55e4bb4ef262f7e92c46de2f4 to your computer and use it in GitHub Desktop.
from io import BytesIO
import base64
import gzip
import json
def gzip_b64encode(data):
compressed = BytesIO()
with gzip.GzipFile(fileobj=compressed, mode='w') as f:
json_response = json.dumps(data)
f.write(json_response.encode('utf-8'))
return base64.b64encode(compressed.getvalue()).decode('ascii')
def lambda_handler(event, context):
response = [{'name': 'Momo', 'age': 2}]
return {
'isBase64Encoded': True,
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
'Access-Control-Allow-Origin': '*'
},
'body': gzip_b64encode(response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment