[英]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 在获取单个项目时要快得多。
user = "jen"
be made into dictionary?应该将user = "jen"
制作成字典吗? What is the syntax?语法是什么?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.