[英]Getting error Invalid type for parameter Item, value: type: <class 'list'>, valid types: <class 'dict'> while loading S3 to DynamoDB
I am new to AWS and learning.我是 AWS 和学习的新手。 I have a JSON file with details of employees in S3 folder.我有一个 JSON 文件,其中包含 S3 文件夹中员工的详细信息。 Wrote a Lambda function with PYTHON 3.6 to load DynamoDB table XXXXX-YYYY-ZZZZ-Employees when the JSON file is uploaded to S3.使用 PYTHON 3.6 编写了一个 Lambda 函数,用于在 JSON 文件上传到 S3 时加载 DynamoDB 表 XXXXX-YYYY-ZZZZ-Employees。 All setups have been done with policy and roles to access CloudwatchLog, S3 and DynamoDB.所有设置都已使用策略和角色完成,以访问 CloudwatchLog、S3 和 DynamoDB。 Log file gives the error below.日志文件给出了以下错误。 It appears that the issue is the file is a dictionary format like [] and the put_item expects string.. not sure.看来问题是文件是像 [] 这样的字典格式,而 put_item 需要字符串..不确定。
(empid is the primary key) (empid 是主键)
Reviewed code and researched issue in google and AWS forums在谷歌和 AWS 论坛中审查代码和研究问题
import boto3
import json
s3_client = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
json_file_name = event['Records'][0]['s3']['object']['key']
json_object = s3_client.get_object(Bucket=bucket, Key=json_file_name)
jsonFileReader = json_object['Body'].read()
#print(jsonFileReader)
jsonDict = json.loads(jsonFileReader)
#print(jsonDict)
table = dynamodb.Table('XXXXXX-YYYYY-ZZZZZZ-Employees')
table.put_item(Item=jsonDict)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Expected result: DynamoDB table loaded with JSON file data Actual Result: getting error in cloudwatchlog:预期结果:DynamoDB 表加载了 JSON 文件数据实际结果:在 cloudwatchlog 中出现错误:
Invalid type for parameter Item, value: [{'empid': 10004, 'email': 'test1@email.com', 'firstname': 'John5', 'lastname': 'Smith5', 'location': 'ABCD-B207', 'mobile': '123-456-7890', 'work': '098-765-4321', 'site': 'ABCD'}, {'empid': 10005, 'email': 'test1@email.com', 'firstname': 'John6',................. '}], type: , valid types:参数项的类型无效,值:[{'empid':10004,'email':'test1@email.com','firstname':'John5','lastname':'Smith5','location':'ABCD -B207','移动':'123-456-7890','工作':'098-765-4321','站点':'ABCD'},{'empid':10005,'email':'test1' @email.com', 'firstname': 'John6',................ '}], type: , 有效类型:
Your programming is reading the entire file into jsonDict
.您的编程正在将整个文件读入jsonDict
。 You are then attempting to insert one item using the dictionary.然后您尝试使用字典插入一项。
Instead, you should extract one line at a time, then use put_item()
to insert one item.相反,您应该一次提取一行,然后使用put_item()
插入一项。 Repeat for each line.对每一行重复。
Something like this might be possible:像这样的事情可能是可能的:
jsonDict = json.loads(jsonFileReader)
table = dynamodb.Table('XXXXXX-YYYYY-ZZZZZZ-Employees')
for item in jsonDict:
table.put_item(Item=item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.