AWS firehose lambda函数调用给出了错误的输出结构格式

时间:2018-12-13 09:00:24

标签: python-3.x amazon-web-services aws-lambda amazon-kinesis amazon-kinesis-firehose

当我使用put操作将数据对象插入aws firhose流时,它可以正常工作。由于我的firehose流上启用了lambda函数。因此,调用了lambda函数,但给了我输出结构响应错误:

"errorMessage":"Invalid output structure: Please check your function and make sure the processed records contain valid result status of Dropped, Ok, or ProcessingFailed."

所以现在我以这种方式创建了lambda函数,以使输出结构正确:

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 }

现在,我在将“数据”字段编码为

时遇到了错误
"errorMessage": "a bytes-like object is required, not 'str'",

如果我将'hello'更改为b'hello'之类的字节,则会出现以下错误:

 "errorMessage": "Object of type bytes is not JSON serializable",

1 个答案:

答案 0 :(得分:0)

导入json 导入base64 导入gzip 进口io 导入zlib

def lambda_handler(事件,上下文):     输出= []

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}