【问题标题】:How to upload files with size more than 30000000 Bytes (28.6MB) to aws ec2 server (.net-core web api project)如何将大小超过 30000000 字节(28.6MB)的文件上传到 aws ec2 服务器(.net-core web api 项目)
【发布时间】:2021-05-30 21:07:41
【问题描述】:

错误

当我从 swagger ui 测试文件大小超过 28.6MB(例如 30MB)时,我收到以下错误响应:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>502 Bad Gateway</title>
</head><body>
<h1>Bad Gateway</h1>
<p>The proxy server received an invalid
response from an upstream server.<br />
</p>
<hr>
<address>Apache/2.4.29 (Ubuntu) Server at ec2-00-00-0-000.compute-1.amazonaws.com Port 80</address>
</body></html>

项目信息

  • 项目类型:dot net core web api (C#)
  • 托管在:aws (ec2)

到目前为止我做了什么

我添加了一个web.config 文件,内容如下

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>

</configuration>

我在Startup.cs 文件中添加了以下内容

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<IISServerOptions>(options =>
    {
        options.MaxRequestBodySize = int.MaxValue;
    });

    services.Configure<FormOptions>(x =>
    {
        x.ValueLengthLimit = int.MaxValue;
        x.MultipartBodyLengthLimit = int.MaxValue;
        x.MultipartHeadersLengthLimit = int.MaxValue;
    });
    ...
    ...
    ...
}

测试模型类:

public class TestModel
{
    public IFormFile TestFile { get; set; }
}

测试控制器方法:

[Route("uploadtest")]
[HttpPost]
[Consumes("multipart/form-data")]
public async Task<ActionResult> UploadTest([FromForm] TestModel fff)
{
    return Ok();
}

额外信息

当我从 Visual Studio (IIS Express) 在本地电脑上运行它时,我没有收到错误响应。

【问题讨论】:

    标签: c# .net-core amazon-ec2 file-upload


    【解决方案1】:

    您不会在 IIS Express 中收到错误,因为问题不在于项目,而在于运行它的托管服务器。在这种情况下,使用 aws ec2。默认的最大文件上传为 2MB,因此超出此范围的任何内容都不会通过。您必须修改主机服务器中的配置。

    这是一个希望能帮助解决问题的链接: Amazon EC2 Ubuntu Instance Maximum File Upload Size

    【讨论】:

      【解决方案2】:

      我终于能够通过如下设置 KestrelServerOptions 来解决它:

      services.Configure<KestrelServerOptions>(options =>
      {
          options.Limits.MaxRequestBodySize  = int.MaxValue; 
      });
      

      由于 aws 使用的是 kestrel,这就是为什么问题只发生在 aws 中,而不是在我的本地 IISExpress 服务器中。

      感谢:https://stackoverflow.com/a/60964837/765416

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-02
        • 1970-01-01
        • 2020-05-23
        • 2021-03-07
        • 2020-02-02
        • 2020-12-08
        • 2020-10-29
        • 2018-06-24
        相关资源
        最近更新 更多