AWS ApiGateway默认会在4 KB处分割消息。如果希望更改这个默认设置,可以在集成请求或响应的“设置”选项卡中配置负载分割选项。以下是一个使用Lambda函数处理分割消息的示例代码:
exports.handler = async (event) => {
let data = '';
for (const record of event.Records) {
data += record.body;
if (record.attributes != null && record.attributes.ApproximateReceiveCount >= 3) {
// This message has been received multiple times, it may be a poison pill - consider removing it from the queue
}
}
console.log('Received data:', data);
return {};
};
在这个示例中,我们使用循环来将所有分段的消息组合在一起,然后输出到控制台。注意,由于AWS Lambda函数默认会接受最大128MB的数据,因此在实际使用时可能需要进行限制和优化。