AWS firehose lambda函数调用给出了错误的输出结构格式 [英] aws firehose lambda function invocation gives wrong output strcuture format
问题描述
当我使用put操作将数据对象插入aws firhose流时,它可以正常工作.由于我的firehose流上启用了lambda函数.因此,调用了lambda函数但给了我输出结构响应错误:
When i insert a data object to aws firhose stream using a put operation it works fine .As lambda function is enabled on my firehose stream .hence a lambda function is invoked but gives me a output structure response error :
"errorMessage":"Invalid output structure: Please check your function and make sure the processed records contain valid result status of Dropped, Ok, or ProcessingFailed."
所以现在我以这种方式创建了lambda函数,以实现正确的输出结构:
so now i have created my lambda function like this way to make the correct output strcuture :
import base64
import json
print('Loading function')
def lambda_handler(event, context):
output=[]
print('event'+str(event))
for record in event['records']:
payload = base64.b64decode(record['data'])
print('payload'+str(payload))
payload=base64.b64encode(payload)
output_record={
'recordId':record['recordId'],
'result': 'Ok',
'data': base64.b64encode(json.dumps('hello'))
}
output.append(output_record)
return { 'records': output }
现在,我在将数据"字段编码为
Now i am getting the follwing eror on encoding the 'data' field as
"errorMessage": "a bytes-like object is required, not 'str'",
如果我将'hello'更改为类似b'hello'的字节,则会出现以下错误:
and if i change the 'hello' to bytes like b'hello' then i get the following error :
"errorMessage": "Object of type bytes is not JSON serializable",
推荐答案
import json导入base64导入gzip进口io导入zlib
import json import base64 import gzip import io import zlib
def lambda_handler(事件,上下文):输出= []
def lambda_handler(event, context): output = []
for record in event['records']:
payload = base64.b64decode(record['data']).decode('utf-8')
output_record = {
'recordId': record['recordId'],
'result': 'Ok',
'data': base64.b64encode(payload.encode('utf-8')).decode('utf-8')
}
output.append(output_record)
return {'records': output}
这篇关于AWS firehose lambda函数调用给出了错误的输出结构格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!