AWS Lambda next.js - 无法 post 2MB 文件到 api 路由

AWS Lambda next.js - Failed to post 2MB file to api route

我使用serverless-next.js 包将我的next.js 应用程序部署到aws。 当我尝试 post 一个大文件 (~1MB) 到 api 路由时,我得到一个 503 响应代码。 完整回复内容:

<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>503 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: X
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

CloudWatch 日志显示:

{
    "errorType": "Error",
    "errorMessage": "Cannot call write after a stream was destroyed",
    "code": "ERR_STREAM_DESTROYED",
    "stack": [
        "Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed",
        "    at WriteStream._write (internal/fs/streams.js:379:33)",
        "    at WriteStream.<anonymous> (internal/fs/streams.js:375:12)",
        "    at Object.onceWrapper (events.js:422:26)",
        "    at WriteStream.emit (events.js:327:22)",
        "    at WriteStream.EventEmitter.emit (domain.js:483:12)",
        "    at internal/fs/streams.js:366:10",
        "    at FSReqCallback.oncomplete (fs.js:156:23)"
    ]
}

我正在使用 formidable 来解析请求的正文。它适用于较小的文件大小。

我能做什么? 1MB 不算大文件。

在开发中,即使是 10MB 的文件也能正常工作,但在 AWS lambda@edge

写在这里:https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html

如果您以异步模式上传,您的请求和响应不能超过 256 KB。

因为无服务器仅用于在 lambda 函数中创建路由。如果要上传此文件,可以使用带有预签名 url 的 S3 存储桶。 这样您就不需要将文件上传到 lambda 函数。