简体   繁体   English

参数验证失败:参数 Key 的类型无效。,值:,类型:<class 'str'> ,有效类型:<class 'dict'></class></class>

[英]Parameter validation failed: Invalid type for parameter Key., value: , type: <class 'str'>, valid types: <class 'dict'>

I have 2 Lambda, 1 is doing a batch_write and put_item to ddb.我有 2 个 Lambda,其中 1 个正在对 ddb 执行 batch_write 和 put_item。 The other lambda does the get_item from first lambda (It has permissions to get_item).另一个 lambda 从第一个 lambda 执行 get_item(它具有 get_item 的权限)。

ERROR:错误:

[ERROR] ParamValidationError: Parameter validation failed:
Invalid type for parameter Key.active_employee, value: jen, type: <class 'str'>, valid types: <class 'dict'>
Traceback (most recent call last):
  File "/var/task/my_lambda/checks.py", line 100, in lambda_handler
    response = ddb.get_item(TableName="testtable", Key={"active_employee": user})

Lambda 1: Lambda 1:

        with gzip.open(response["Body"], "rt") as file:
        try:

            with table.batch_writer(overwrite_by_pkeys=["active_employee"]) as batch:
                for active_users in file:
                    user_dict = json.loads(active_users)
             
                    manager = user_dict["manager"]
                    user = user_dict["user"]


                    if not manager:
                        continue


                    if not user:
                        continue
                 
        
                    else:
                        batch.put_item(
                            Item={
                                "active_employee": user,
                                "mgr_email": mgr_email
            
                            },
                        )

            logger.info("Loaded data into table %s.", table.name)

        except ClientError:
            logger.exception("Couldn't load data into table %s.", table.name)
            raise

Lambda 2 Lambda 2


user = "jen"

    ddb = boto3.client("dynamodb")
    response = ddb.get_item(TableName="testtable", Key={"active_employee": user})
    employee_data = json.loads(response["Item"])
    if employee_data and employee_data["active_employee"] == user:
        manager = employee_data["mgr_email"]
        print(f"{user} is active")
        print(f"{manager}")
    else:
        print("user not in ddb")

I am expecting to get in Lambda jen is active then the manager email. I do not know the manager value.我期待进入 Lambda jen is active的,然后是经理 email。我不知道经理的价值。 Say the DDB has a million in it and I cannot use scan or query.假设 DDB 中有一百万,我不能使用扫描或查询。 I've read that get_item is a lot faster when getting a single item.我读过 get_item 在获取单个项目时要快得多。

  • How can I fix the error?我该如何修复错误?
  • How do I get_item as dictionary?如何将 get_item 作为字典? should the user = "jen" be made into dictionary?应该将user = "jen"制作成字典吗? What is the syntax?语法是什么?
  • When doing get_item can I only use the pk and expect to also get the other key (mgr_email).在执行 get_item 时,我只能使用 pk 并期望获得另一个密钥 (mgr_email)。 I can only get_item the user and I need it to look for the manager email for me of that alias too if it exists and return both.我只能 get_item 用户,我需要它为我寻找该别名的经理 email(如果它存在)并返回两者。

In Lambda 2 you are using the low level client, which expects DynamoDB JSON such as:在 Lambda 2 中,您使用的是低级客户端,它需要 DynamoDB JSON,例如:

{'active_employee':{'S':'jen'}}

Now, for you to make it work in your current context, you would be better using the Resource client, as you do in Lambda 1.现在,为了让它在您当前的上下文中工作,您最好使用Resource客户端,就像您在 Lambda 1 中所做的那样。

dynamodb = boto3.resource("dynamodb", region_name='us-west-2')

table = dynamodb.Table('testtable')

try:
    response = table.get_item(
        Key={
            'active_employee': "jen"
        }
    )
except ClientError as e:
    print(e.response['Error']['Message'])

Be careful not to mix your clients up, and always refer to the documentation for the specific client you are using.注意不要混淆您的客户端,并始终参考您正在使用的特定客户端的文档。

Resource Client: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.get_item资源客户端: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.get_item

Low Level Client: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.get_item低级客户端: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.get_item

暂无
暂无

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

相关问题 参数 AuthParameters.PASSWORD 的类型无效:<class 'list'> ,有效类型:<class 'str'> 由于临时密码中包含保留字符</class></class> - Invalid type for parameter AuthParameters.PASSWORD: <class 'list'>, valid types: <class 'str'> due to reserved char contained in temporary password 执行 PUTITEM 类型时出错:<class 'str'> ,有效类型:<class 'dict'></class></class> - Error when doing a PUTITEM type: <class 'str'>, valid types: <class 'dict'> 如何在Kotlin中获取泛型参数class - How to get class of generic type parameter in Kotlin 一个或多个参数值无效:ComparisonOperator IN 对 SS AttributeValue 类型无效 - One or more parameter values were invalid: ComparisonOperator IN is not valid for SS AttributeValue type Class如何转换<str>至 Class<dict></dict></str> - How to convert Class<str> to Class<dict> “User”类型的参数不可分配给“FirebaseOperation”类型的参数 - Argument of type 'User' is not assignable to parameter of type 'FirebaseOperation' 如何在 PowerShell CLI 提示中为复杂类型的参数提供值 - How to Provide a Value in PowerShell CLI Prompt for a Parameter of Complex Type biqquery 中结构数组的命名参数类型 - Named parameter type for Array of Struct in biqquery DynamoDB:从 cli 创建具有全局索引的表 - 参数验证失败 - DynamoDB: create table with global index from cli - parameter validation failed “this”类型的参数不可分配给 AWS CDK 中“Construct”类型的参数 - Argument of type 'this' is not assignable to parameter of type 'Construct' in AWS CDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM