首页 > 解决方案 > aws firehose lambda 函数调用给出错误的输出结构格式

问题描述

当我使用 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",

标签: python-3.xamazon-web-servicesaws-lambdaamazon-kinesisamazon-kinesis-firehose

解决方案


导入 json 导入 base64 导入 gzip 导入 io 导入 zlib

def lambda_handler(event, context): 输出 = []

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}

推荐阅读