AWS SQS 异常“413 请求实体太大”

AWS SQS Exception "413 Request Entity Too Large"

我已经被困了一段时间以弄清楚以下异常,我们将提供任何帮助。 我们在 Laravel 项目上使用 AWS Lambda 服务。我们正在使用 Laravel Excel 将大量数据导出到 CSV 文件,我们正在通过 Laravel SQS 队列执行此操作。

php 版本:7.2
Laravel 框架:7.30.1
Laravel Excel : 3.1

异常:

Aws\Sqs\Exception\SqsException /tmp/vendor/aws/aws-sdk-php...
stage.ERROR: Error executing "SendMessage" on "https://sqs.eu-central- 
1.amazonaws.com"; AWS HTTP error: Client error: `POST https://sqs.eu- 
central-1.amazonaws.com/` resulted in a `413 Request Entity Too Large` 
response:
HTTP content length exceeded 1662976 bytes.
Unable to parse error information from response - Error parsing XML: String could not be parsed as 
XML {"exception":"[object] (Aws\Sqs\Exception\SqsException(code: 0): Error executing      
\"SendMessage\" on \"https://sqs.eu-central-1.amazonaws.com/"; AWS 
HTTP error: Client error: `POST https://sqs.eu-central-1.amazonaws.com/` resulted in a `413 Request Entity Too Large` response:
HTTP content length exceeded 1662976 bytes.
Unable to parse error information from response - Error parsing XML: String could not be parsed as 
XML at /tmp/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php:195)
[stacktrace]

来自docs

Message size
The minimum message size is 1 byte (1 character). The maximum is 262,144 bytes (256 KB).

我的猜测是您尝试发送的文件大于 256KB,这是 AWS 中 SQS 消息的硬性限制。

输出 HTTP content length exceeded 1662976 bytes 表明我的猜测可能是正确的。

这种情况下的常见模式是将文件上传到 S3 并通过队列发送对对象的引用。

场景
由于 AWS docs hard limit to send data to SQS queue is 256KB. that was happening because I was sending Collection object to the SQS Queue, and each object had Multiple Eager Loading 关系所以对象大小变得太大。

AWS 解决方案
将集合对象数组中的 ID 传递给 SQS 队列有助于解决 Entity Too Large 问题。

Excel解决方案
而不是使用 Laravel Excel I am using Laravel Storage append 方法将行添加到我的 csv 文件。我已经测试了 50k 行并且效果很好。不要一次处理所有行,您可能会陷入 AWS timeout 异常,我相信每个队列 15 minutes