我有以下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