代码之家  ›  专栏  ›  技术社区  ›  divyanayan awasthi

aws firehose lambda函数调用提供了错误的输出结构格式

  •  0
  • divyanayan awasthi  · 技术社区  · 5 年前

    "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 回复  |  直到 5 年前
        1
  •  0
  •   Sekhar C    5 年前

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

    def lambda_处理程序(事件、上下文):

    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}