繁体   English   中英

使用 Python 将 JSON 数据插入 DynamoDB 时出错

[英]Getting Error While Inserting JSON data to DynamoDB using Python

    HI,

    I am trying to put the json data into AWS dynamodb table using AWS Lambda , however i am getting an error like below. My json file is uploaded to S3 bucket.

    Parameter validation failed:
    Invalid type for parameter Item, value:
    {
        "IPList": [
            "10.1.0.36",
            "10.1.0.27"
        ],
        "TimeStamp": "2020-04-22 11:43:13",
        "IPCount": 2,
        "LoadBalancerName": "internal-ALB-1447121364.us-west-2.elb.amazonaws.com"
    }
    , type: <class 'str'>, valid types: <class 'dict'>: ParamValidationError

Below i my python script:-

    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()
        jsonDict = json.loads(jsonFileReader)
        table = dynamodb.Table('test')
        table.put_item(Item=jsonDict)

        return 'Hello'

下面是我的json内容

"{\"IPList\": [\"10.1.0.36\", \"10.1.0.27\"], \"时间戳\": \"2020-04-22 11:43:13\", \"IPCount \": 2, \"LoadBalancerName\": \"internal-ALB-1447121364.us-west-2.elb.amazonaws.com\"}"

有人可以帮助我,如何将数据插入 dynamodb。

json.loads(jsonFileReader)返回string ,但table.put_item()需要dict 请改用json.load()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM