更新带有python中lambda中DynamoDB表的值的JSON文件

发布于 2025-02-02 00:12:44 字数 3104 浏览 2 评论 0原文

我有一个包含以下元素的JSON文件:ID,FIRSTNAME,LASTNAME,电子邮件 我也有一个具有相同字段的DynamoDB表。这就是我想做的。

每次我将JSON文件上传到S3存储桶中时,我都会触发lambda lambda将将JSON中的ID与DynamoDB表中的ID进行比较。
如果IDS匹配,则更新firstName,lastname和emaim in JSON文件
将JSON文件输出到另一个S3桶

AWS lambda函数

import json
import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):
    ##Get data from s3 bucket
    bucket = 'test-bucket'
    key = 'input.json'
    
    s3response = s3.get_object(Bucket = bucket, Key = key)
    content = s3response['Body']
    s3data = json.loads(content.read())
    
    ##Get data from DynamoDB table
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('mytable')

    dbresponse = table.scan()
    dynamodbdata = dbresponse['Items']

    ##Update values in s3 json with values from DynamoDB
    s3data.update((k,v) for k,v in dynamodbdata.items())
    
    #Return new dictionary for s3data
    return s3data

示例JSON文件以下:

[{"id":1,"first_name":"Nikolas","last_name":"Philipard","email":"[email protected]"},
{"id":2,"first_name":"Al","last_name":"Harden","email":"[email protected]"},
{"id":3,"first_name":"Lamar","last_name":"Joinsey","email":"[email protected]"},
{"id":4,"first_name":"Vicky","last_name":"Picker","email":"[email protected]"},
{"id":5,"first_name":"Thomasina","last_name":"Trowl","email":"[email protected]"},
{"id":6,"first_name":"Lyon","last_name":"Vassay","email":"[email protected]"},
{"id":7,"first_name":"Terrye","last_name":"Mereweather","email":"[email protected]"},
{"id":8,"first_name":"Kathryne","last_name":"d'Escoffier","email":"[email protected]"},
{"id":9,"first_name":"Ewart","last_name":"Hebden","email":"[email protected]"},
{"id":10,"first_name":"Mellisa","last_name":"Feetham","email":"[email protected]"}]

I have a json file containing the following elements: id, firstname, lastname, email
I also have a dynamodb table with the same fields. This is what I would like to do.

Each time I upload the json file into an s3 bucket, I trigger a lambda
Lambda will compare ids in the json to those in the dynamodb table.
If ids match then update firstname, lastname and email in json file
Output the json file to another s3 bucket

AWS Lambda Function

import json
import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):
    ##Get data from s3 bucket
    bucket = 'test-bucket'
    key = 'input.json'
    
    s3response = s3.get_object(Bucket = bucket, Key = key)
    content = s3response['Body']
    s3data = json.loads(content.read())
    
    ##Get data from DynamoDB table
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('mytable')

    dbresponse = table.scan()
    dynamodbdata = dbresponse['Items']

    ##Update values in s3 json with values from DynamoDB
    s3data.update((k,v) for k,v in dynamodbdata.items())
    
    #Return new dictionary for s3data
    return s3data

Sample json file below:

[{"id":1,"first_name":"Nikolas","last_name":"Philipard","email":"[email protected]"},
{"id":2,"first_name":"Al","last_name":"Harden","email":"[email protected]"},
{"id":3,"first_name":"Lamar","last_name":"Joinsey","email":"[email protected]"},
{"id":4,"first_name":"Vicky","last_name":"Picker","email":"[email protected]"},
{"id":5,"first_name":"Thomasina","last_name":"Trowl","email":"[email protected]"},
{"id":6,"first_name":"Lyon","last_name":"Vassay","email":"[email protected]"},
{"id":7,"first_name":"Terrye","last_name":"Mereweather","email":"[email protected]"},
{"id":8,"first_name":"Kathryne","last_name":"d'Escoffier","email":"[email protected]"},
{"id":9,"first_name":"Ewart","last_name":"Hebden","email":"[email protected]"},
{"id":10,"first_name":"Mellisa","last_name":"Feetham","email":"[email protected]"}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文