使用AWS python lambda读取表单数据

问题描述 投票:0回答:1

我想阅读http post请求的“key”参数,但它不起作用。

def my_handler(event, context):
    print(event)
    print(event['body'])
    print("key: " + event['key'])

    key = event['query']['key']

    encoded_string = str(key).encode("utf-8")
    # Create the file named for example "42.json" containing the appropriate data
    s3_path =  str(key) + '.json'
    s3 = boto3.resource("s3")
    s3.Bucket(BUCKET_NAME).put_object(Key=s3_path, Body=encoded_string)

    message = {
       'message': 'Created {}!'.format(key)  
    }
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(message)
    }

更新:如果我使用下面的代码,我可以在http帖子中读取JSON数据,但我仍然无法读取表单数据。

def my_handler(event, context):
    print(event)
    print(event['body'])
   # print("key: " + event['key'])
    print("key  " + json.loads(event['body'])["key"])

    key = json.loads(event['body'])["key"]

    encoded_string = str(key).encode("utf-8")
    # Create the file named for example "42.json" containing the appropriate data
    s3_path =  str(key) + '.json'
    s3 = boto3.resource("s3")
    s3.Bucket(BUCKET_NAME).put_object(Key=s3_path, Body=encoded_string)

    message = {
       'message': 'Created {}!'.format(key)  
    }
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(message)
    }
python-3.x aws-lambda
1个回答
2
投票

enter image description here 1.我假设你正在使用API​​网关,然后使用Lambda代理集成,它在你的API网关上的集成请求下。 aws docs

  1. 如果您使用此设置,则不需要执行任何映射。
© www.soinside.com 2019 - 2024. All rights reserved.