" KeyError:“记录""在AWS S3中-Lambda触发器 [英] "KeyError: 'Records'" in AWS S3 - Lambda trigger
问题描述
我有以下lambda函数代码,用于简单地打印出S3存储桶中已上传事件的作者和元数据:
I have the following lambda function code for simply printing out the Author and metadata of an uploaded event of an S3 bucket:
from __future__ import print_function
import json
import urllib
import boto3
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# bucket = event['Records'][0]['s3']['bucket']['name']
for record in event['Records']:
bucket = record[0]['s3']['bucket']['name']
key = record[0]['s3']['object']['key']
response = s3.head_object(Bucket=bucket, Key=key)
logger.info('Response: {}'.format(response))
print("Author : " + response['Metadata']['author'])
print("Description : " + response['Metadata']['description'])
但是,我在测试时遇到以下错误:
However, I am getting the following error while testing:
{
"stackTrace": [
[
"/var/task/lambda_function.py",
17,
"lambda_handler",
"for record in event['Records']:"
]
],
"errorType": "KeyError",
"errorMessage": "'Records'"
}
访问S3对象的存储桶名称和键名时,我做错什么了吗?如果没有,那我在做什么错了?
Am I doing anything wrong while accessing the bucket name and key name of the S3 object? If not, then what am I doing wrong here?
推荐答案
晚了一点.但是,这是我的第一篇文章!
bit late to the party. But here is my first post!
EXPLANATION:
当您在lambda面板中进行测试时-> def lambda_handler(event,context)<-事件将直接注入.
When you test in lambda panel -> def lambda_handler(event, context) <- event is injected directly.
但是在AWS API中,必须添加映射模板或否则->事件<-为空,从而导致测验:
However in AWS API its neccessary to add Mapping Template or otherwise -> event <- is empty, thus resulting in quizzing:
"errorType":"KeyError","errorMessage":'Records'"
"errorType": "KeyError", "errorMessage": "'Records'"
这是空指针.记录不存在,因为->事件<-不存在.
this is null pointer. Records doesnt exist, since -> event <- doesnt exist.
解决方案:
您需要在AWS API中配置集成请求. 点击身体映射模板. 然后添加映射模板 将内容类型设置为 application/json 然后编辑生成的映射模板:
You need to configure Integration Request inside AWS API. Click on Body Mapping Templates. Then add Mapping Template Set content type to application/json Then edit generated mapping template:
{
"body" : $input.json('$'),
"headers": {
#foreach($header in $input.params().header.keySet())
"$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
#end
},
"method": "$context.httpMethod",
"params": {
#foreach($param in $input.params().path.keySet())
"$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end
#end
},
"query": {
#foreach($queryParam in $input.params().querystring.keySet())
"$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end
#end
}
}
和编辑Lambda函数:
替换:
用于事件['Records']中的记录:
for record in event['Records']:
具有:
用于事件['query'] ['Records']
for record in event['query']['Records']
不知道堆栈是否可以ping通您这个答案-所以我称呼您@ Dawny33 @KevinOelen @franklinsijo
don't know whether stack will ping you with this answer - so i call you @Dawny33 @KevinOelen @franklinsijo
至于解释,我自己想了一下.但是,映射模板"来自 https://medium.com/simple-thoughts-amplified/passing-variables-from-aws-api-gateway-to-lambda-3c5d8602081b
As for explanation i figured it on my own. However "mapping template" comes from https://medium.com/simple-thoughts-amplified/passing-variables-from-aws-api-gateway-to-lambda-3c5d8602081b
这篇关于" KeyError:“记录""在AWS S3中-Lambda触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!