简体   繁体   English

AWS Lambda Webhook 事件处理,NodeJS 到 Python

[英]AWS Lambda Webhook event handling, NodeJS to Python

I got this NodeJS code from a coworker.我从同事那里得到了这个 NodeJS 代码。 This code is in AWS lambda and handles incoming events which are sent per webhook.此代码在 AWS lambda 中并处理每个 webhook 发送的传入事件。 Inside this code I specify a Kinesis Firehose delivery stream which then sends the data to a bucket.在此代码中,我指定了 Kinesis Firehose 传输流,然后将数据发送到存储桶。 I have the delivery stream in between to do some zipping and adding partition.我在两者之间有传输流来做一些压缩和添加分区。 2nd thing that is important in this code is the part where I take the body of the incoming event and send it as json to the delivery stream.在这段代码中重要的第二件事是我获取传入事件的主体并将其作为 json 发送到传递流的部分。

Can someone help me to interpret this code to python?有人可以帮我将此代码解释为 python 吗?

 const AWS = require( 'aws-sdk' ); const firehose = new AWS.Firehose(); exports.handler = async (event) => { console.log(JSON.stringify(event, null, 4)); try { await firehose .putRecord({ DeliveryStreamName: 'foo', Record: { Data:event.body } }) .promise(); } catch (error) { console.error(error); return { statusCode: 400, body: `Cannot process event: ${error}` }; } return { statusCode: 200, body: JSON.stringify({ ack: new Date().getTime() }) }; };

You can use Boto3 Python AWS SDK您可以使用Boto3 Python AWS 开发工具包

import json
import boto3
from datetime import datetime

client = boto3.client('firehose')

def handler(event): 
    print(json.dumps(event, indent=4))
    try:
        response = client.put_record(
        DeliveryStreamName='string',
        Record={
            'Data': event.body
        }
    )
    except Exception as e:
        return {
            'statusCode': 400,
            'body': json.dumps(f'Cannot process event: {e}')
        }

    return {
        'statusCode': 200,
        'body': json.dumps({ 'ack': datetime.now().strftime('%H:%M:%S') })
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM