我正在使用Python 3.7在AWS Lambda中设置一个函数,但它不允许我返回字节类型。
请注意,这并不是API Gateway的问题,我是直接调用lambda。
错误是:Runtime.MarshalError,... 不可JSON序列化。
请注意,这并不是API Gateway的问题,我是直接调用lambda。
错误是:Runtime.MarshalError,... 不可JSON序列化。
output = BytesIO()
#Code that puts an excel file into output...
return {
'Content-Disposition': 'attachment; filename="export.xlsx"',
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'body' : output.getvalue()
}
如果我执行:
'body' : str(output.getvalue())
由于在字符串后面添加了b''
,导致输出一个损坏的文件。
如果我这样做:
'body' : base64.b64encode(output.getvalue()).decode()
此外,它还会输出一个损坏的文件,可能是因为它改变了文件的二进制表示。
也许我需要上传到S3?但它不符合我的流程,这是一次性文件创建,而且文件将停留在"S3 Limbo"中直到TTL。
output.getvalue().decode('utf-8')
可以解决“不可JSON序列化”的问题。这样做有帮助吗? - Martin Valgur