如何在不将请求发送到原点的情况下回应查看者?

问题描述

我正在尝试在python中实现一个lambda边缘,该边缘会接收文件,并根据内容的长度对其进行评估;如果该文件的长度大于允许的长度,请在不向请求源发送请求的情况下响应查看器,否则它将被发送到S3。我已经实现了要发送到S3的部分,但是在响应查看者方面却很挣扎。

解决方法

您可以为请求事件触发器生成自定义响应

示例:

import json

def lambda_handler(event,context):
    if True: # Case where you want to return the response
        response = {
            'body': "SAMPLE RESPONSE",# For image set this to base64 encoded string
            'bodyEncoding': 'text','status': '200','statusDescription': 'OK','headers': {
            'content-type':
                [
                    {
                    'key': 'Content-Type','value': 'text'
                    }
                ]
            }
        }

        return response
    else: # Case where you want the request to be forwarded to the origin server
        return event['Records'][0]['cf']['request']

注意:如果返回请求事件的响应,则响应大小会有一些限制。可以在Amazon developer guide

上找到它们