简体   繁体   English

将 python 用于 dynamodb 表中的 put_item 与 Lambda function

[英]Using python for put_item in dynamodb table with Lambda function

Writing my first Lambda funcation to put item with Python编写我的第一个 Lambda 函数以使用 Python 放置项目

The problem im having - not getting the input from the registration form ( front end hosted on S3 bucket with static webhost enabled ) to the DynamoDB table我遇到的问题 - 没有从注册表单(前端托管在启用了 static 网络主机的 S3 存储桶上)获取 DynamoDB 表的输入

the Data will be sent with this funcation to the API hosted on AWS数据将通过此功能发送到 AWS 上托管的 API

const BASE_URL = `API-URL`;

function handleForm() {
    const name = document.querySelector('#name').value;
    const email = document.querySelector('#email').value;
    const phone = document.querySelector('#phone').value;
    const data = {
        name,
        email,
        phone
    }
    console.log(data);
    saveDataToAWS(data);
}

async function saveDataToAWS(data) {
    const result = await axios.post(BASE_URL, data);
    return result.data;
}

Im not sure im using AXIOS the right way but lets continue我不确定我是否以正确的方式使用 AXIOS 但让我们继续

The Lambda funcation im using now is pretty much this:我现在使用的 Lambda 函数几乎是这样的:

import json
import boto3

dynamodb=boto3.resource('dynamodb')
table=dynamodb.Table('register')

def lambda_handler(event, context):
    table.put_item(
        Item={
            'name'=event['name'],
            'email'=event['email'],
            'phone'=event['phone']
        }
    )
    respone={
        'mes':'Good input !'
    }
    return {
        'statusCode': 200,
        'body': respone
    }

Im pretty much 99% new to writting code in AWS so im sure im doing most of it wrong Really looking for you help !我几乎 99% 都是在 AWS 中编写代码的新手,所以我确信我做错了大部分真的在寻求你的帮助!

The 'event' attribute has a 'body' parameter that will contain the data in your example: 'event' 属性有一个 'body' 参数,它将包含您示例中的数据:

data = json.loads(event["body"])

table.put_item(
        Item={
            'name':data['name'],
            'email':data['email'],
            'phone':data['phone']
        }
    )

Remember to check CloudWatch Logs as well, as it will tell you whether the Lambda was invoked in the first place, and if it failed.请记住还要检查 CloudWatch Logs,因为它会告诉您是否首先调用了 Lambda,以及它是否失败。

More information on the structure of the event -attribute can be found here: https://aws-lambda-for-python-developers.readthedocs.io/en/latest/02_event_and_context/可以在此处找到有关event属性结构的更多信息: https://aws-lambda-for-python-developers.readthedocs.io/en/latest/02_event_and_context/

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

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