【问题标题】:aws firehose lambda function invocation gives wrong output strcuture formataws firehose lambda 函数调用给出错误的输出结构格式
【发布时间】:2018-12-13 09:00:23
【问题描述】:

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

【问题讨论】:

  • 解决方案'data': base64.b64encode('hello'.encode('utf-8')).decode('utf-8').处理/更新后的数据可以作为字符串传递在将数据发送回队列时代替单词“hello”对数据进行编码

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


【解决方案1】:

导入 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}

【讨论】:

    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多