简体   繁体   English

获取错误参数 Item 的类型无效,值:类型:<class 'list'> , 有效类型:<class 'dict'> 将 S3 加载到 DynamoDB 时

[英]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.

相关问题 参数验证失败:参数 Key 的类型无效。,值:,类型:<class 'str'> ,有效类型:<class 'dict'></class></class> - Parameter validation failed: Invalid type for parameter Key., value: , type: <class 'str'>, valid types: <class 'dict'> 参数验证失败:\\n参数 Filters[0].Values[0] 的类型无效,值:{},类型:<class 'dict'> , 有效类型:<class 'str'> - Parameter validation failed:\nInvalid type for parameter Filters[0].Values[0], value: {}, type: <class 'dict'>, valid types: <class 'str'> FastAPI 给出“value is not a valid dict (type=type_error.dict)” - FastAPI gives "value is not a valid dict (type=type_error.dict)" Python s3 到 Redshift 错误:无效数字,值 '"',位置 0,类型: - Python s3 to Redshift error: Invalid digit, Value '"', Pos 0, Type: python dynamodb - 参数类型无效 - python dynamodb - Invalid type for parameter Boto 3 DynamoDB batchWriteItem 指定类型时属性值类型无效 - Boto 3 DynamoDB batchWriteItem Invalid attribute value type when specifying types ValueError:无效的文件路径或缓冲区对象类型:<class 'dict'> Python - ValueError: Invalid file path or buffer object type: <class 'dict'> python ValueError:无法引用类型的参数值[] <class 'list'> Django的ArrayField - ValueError: Cannot quote parameter value [] of type <class 'list'> Django ArrayField python: {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} - python : {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} “ TypeError:未知参数类型: <class 'dict_values'> ” - “TypeError: Unknown parameter type: <class 'dict_values'>”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM