更新带有python中lambda中DynamoDB表的值的JSON文件
我有一个包含以下元素的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论