python-3.x - 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",
解决方案
导入 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}
推荐阅读
- swift - 在 UITabBarController 中的 tabBar 和 Navigation 视图之间添加 UIView
- html - 模态正文文本流出 - 自动换行不起作用
- python - Python 中的 JSON 导入
- java - 通过命令行选择 application.properties 文件
- batch-file - 如何通过批处理文件将驱动器映射为系统帐户
- c# - 流上需要一个进程中的“管道”
- php - Laravel 任务 - 存储上次运行的时间
- updates - MarkLogic 集群 - 在第一台主机中添加数据并在第二台主机中更新会引发错误
- javascript - 上传图片并停留在页面上
- scala - Scala Play:使用正则表达式路由可选参数?