我有以下lambda函数的代码只需打印出的S3存储的上传事件的作者和元数据:“KeyError异常:‘记录’”在AWS S3 - 拉姆达触发
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'])
然而,我在测试时出现以下错误:
{
"stackTrace": [
[
"/var/task/lambda_function.py",
17,
"lambda_handler",
"for record in event['Records']:"
]
],
"errorType": "KeyError",
"errorMessage": "'Records'"
}
我在访问S3对象的存储桶名称和密钥名称时做了什么错误?如果没有,那么我在这里做错了什么?
您使用的是什么类型的S3事件触发器? – franklinsijo
@franklinsijo'事件类型:ObjectCreated' – Dawny33
这是一个测试事件还是实际事件?你能打印'event'并检查它是否包含'Records'? – franklinsijo