Last active
November 20, 2020 21:35
-
-
Save janrubio/c6d2adf55e4bb4ef262f7e92c46de2f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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