使用 boto3 更新 DynamoDb

DynamoDb update using boto3

我正在编写一个 lambda 函数来更新 dynamodb 项目。但问题是当我给

 {
  "employee_id": "1",
  "last_name": "jane",
  "first_name": "anderson"
}

它工作正常但是如果你删除 last_name 或 first_name 程序 crashes.I 想要让它动态就像我提供 last_name 和 employee_id 它只改变 last_name 并且 first_name 也是如此。如果我提供 first_name 和 last_name 两者都会改变。 Note: employee_id is used here to fetch the data

 {
  "employee_id": "1",
  "last_name": "jane"
}

它给出一个错误说:

{
  "errorMessage": "'first_name'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n    first_name= event['first_name']\n"
  ]
}

Lambda 函数:

import boto3
import json

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

def lambda_handler(event, context):
    employee_id = event['employee_id']
    last_name= event['last_name']
    first_name= event['first_name']
    update = table.update_item(
        Key={
            'employee_id': employee_id
        },
        ConditionExpression= 'attribute_exists(employee_id)',
        UpdateExpression='SET first_name = :val1, last_name = :val2',
        ExpressionAttributeValues={
            ':val1': last_name,
            ':val2': first_name
        }
    )

很少的可能性你可以处理这个问题。如果 first_name 不存在,您可以跳过 DynamoDB 中的文件,或提供一些 default/empty 值。这是 use-case 具体的,取决于您要如何处理丢失的 first_name。如果不允许出现这种情况,您也可以抛出错误。

无论哪种方式,如果 event 中存在 first_name,则会执行 检查

下面是第一个选项的示例

import boto3
import json

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Employee-wahaj')

def lambda_handler(event, context):

    employee_id = event['employee_id']
    last_name = event['last_name']

    UpdateExpression = 'SET last_name = :val1'
    ExpressionAttributeValues = {':val1': last_name }


    if 'first_name' in event:
        
        first_name= event['first_name']
        UpdateExpression = 'SET last_name = :val1, first_name = :val2'
        ExpressionAttributeValues = {
                ':val1': last_name,
                ':val2': first_name
            }

    update = table.update_item(
        Key={
            'employee_id': employee_id
        },
        ConditionExpression= 'attribute_exists(employee_id)',
        UpdateExpression=UpdateExpression,
        ExpressionAttributeValues=ExpressionAttributeValues
    )

名字和姓氏更新

这是一个基本的解决方案(只有几个 if-else-if),因为我不想让示例过于复杂。

import boto3
import json

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Employee-wahaj')

def lambda_handler(event, context):

    employee_id = event['employee_id']

    if 'first_name' in event and 'last_name' not in event:

        first_name = event['first_name']
        UpdateExpression = 'SET first_name = :val1'
        ExpressionAttributeValues = {':val1': first_name }

    elif 'last_name' in event and 'first_name' not in event:

        last_name = event['last_name']
        UpdateExpression = 'SET last_name = :val1'
        ExpressionAttributeValues = {':val1': last_name}

    elif 'first_name' in event and 'last_name' in event:

        last_name = event['last_name']
        first_name= event['first_name']
        UpdateExpression = 'SET last_name = :val1, first_name = :val2'
        ExpressionAttributeValues = {
                ':val1': last_name,
                ':val2': first_name
            }

    else:
        raise ValueError("first_name and last_name not given")


    update = table.update_item(
        Key={
            'employee_id': employee_id
        },
        ConditionExpression= 'attribute_exists(employee_id)',
        UpdateExpression=UpdateExpression,
        ExpressionAttributeValues=ExpressionAttributeValues
    )