繁体   English   中英

AWS Lambda Webhook 事件处理,NodeJS 到 Python

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

我从同事那里得到了这个 NodeJS 代码。 此代码在 AWS lambda 中并处理每个 webhook 发送的传入事件。 在此代码中,我指定了 Kinesis Firehose 传输流,然后将数据发送到存储桶。 我在两者之间有传输流来做一些压缩和添加分区。 在这段代码中重要的第二件事是我获取传入事件的主体并将其作为 json 发送到传递流的部分。

有人可以帮我将此代码解释为 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() }) }; };

您可以使用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