【问题标题】:AWS Lambda next.js - Failed to post 2MB file to api routeAWS Lambda next.js - 无法将 2MB 文件发布到 api 路由
【发布时间】:2020-11-19 05:16:28
【问题描述】:

我使用serverless-next.js 包将我的next.js 应用程序部署到aws。 当我尝试将一个大文件 (~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 中却不行

【问题讨论】:

    标签: aws-lambda next.js serverless


    【解决方案1】:

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

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

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

    【讨论】:

    • 目的是上传图片。流程是客户端将文件发送到我的api路由,api路由使用aws sdk上传到S3。在开发中,即使使用 10MB 文件,一切都可以正常工作,但在 aws lambda@edge 中却不行。
    • 这种工作流程在无服务器中并不好。您需要与云中的数据交互更少。检查该教程serverless.com/blog/s3-one-time-signed-url
    • 好的,谢谢,我会读的!但是为什么 cloudwatch 日志中的错误是“ERR_STREAM_DESTROYED”?
    • 我编辑了我的答案,我希望能回答你的问题
    • “如果您以异步模式上传,则请求+响应不能大于 256 KB”,但我正在成功上传大小为 900KB 的文件。 “异步模式”是什么意思?
    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 2017-06-26
    • 2021-10-18
    • 2021-06-09
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    相关资源
    最近更新 更多