From c161c01322213d46634c7b6461fba470d471c239 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Wed, 27 Jul 2022 00:36:46 +0800 Subject: [PATCH] AWS: special case API gateway to always return gzipped data to avoid binary/base64 unwanted behavior. --- serverless/aws/lambda_function.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/serverless/aws/lambda_function.py b/serverless/aws/lambda_function.py index c2bfb90..a20f586 100644 --- a/serverless/aws/lambda_function.py +++ b/serverless/aws/lambda_function.py @@ -35,7 +35,9 @@ def get_object_bytes(key, offset, length): # Does not work with CloudFront events/Lambda@Edge; see README def lambda_handler(event, context): path = None + is_api_gateway = False if event.get("pathParameters"): + is_api_gateway = True # API Gateway (HTTP or REST) if "proxy" in event["pathParameters"]: path = "/" + event["pathParameters"]["proxy"] @@ -75,15 +77,22 @@ def lambda_handler(event, context): else: raise e - # CloudFront requires decompressed responses from lambda - # in order to implement the Compressed CacheOptimized policy correctly - # as well as Brotli support + headers = {"Content-Type": "application/protobuf"} + if reader.header().metadata.get("compression") == "gzip": - tile_data = gzip.decompress(tile_data) + if is_api_gateway: + # API Gateway requires a compressed response to correctly return binary data + # instead of base64 + headers["Content-Encoding"] = "gzip" + else: + # CloudFront requires decompressed responses from lambda + # in order to implement the Compressed CacheOptimized policy correctly + # as well as Brotli support + tile_data = gzip.decompress(tile_data) return { "statusCode": 200, "body": base64.b64encode(tile_data), "isBase64Encoded": True, - "headers": {"Content-Type": "application/protobuf"}, + "headers": headers, }