【发布时间】:2020-02-28 08:57:46
【问题描述】:
如何使用 Azure APIM 防止大文件请求?
示例:阻止文件大小 > 50MB 的任何 POST 请求
【问题讨论】:
-
如果您可以依赖正确填充 Content-Length 标头,您可以对其进行过滤(我知道,这不是一个完美的解决方案,而是一个简单的解决方案)
标签: azure azure-api-management
如何使用 Azure APIM 防止大文件请求?
示例:阻止文件大小 > 50MB 的任何 POST 请求
【问题讨论】:
标签: azure azure-api-management
您可以对所有 API 应用以下策略。对于每个 POST 请求,策略都会检查 body 大小,如果大小超过 50MB,则会返回状态 413 - Payload Too Large。
<policies>
<inbound>
<base />
<choose>
<when condition="@(context.Request.Method == "POST")">
<set-variable name="bodySize" value="@(context.Request.Headers["Content-Length"][0])" />
<choose>
<when condition="@(int.Parse(context.Variables.GetValueOrDefault<string>("bodySize"))<52428800)">
<!--let it pass through by doing nothing-->
</when>
<otherwise>
<return-response>
<set-status code="413" reason="Payload Too Large" />
<set-body>@{
return "Maximum allowed size for the POST requests is 52428800 bytes (50 MB). This request has size of "+ context.Variables.GetValueOrDefault<string>("bodySize") +" bytes";
}
</set-body>
</return-response>
</otherwise>
</choose>
</when>
</choose>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
【讨论】:
有称为 quota-by-Key 和 quota-by-subscription 的策略,这将帮助用户阻止超出指定带宽的呼叫。请验证此链接以获取更多详细信息。enter link description here
【讨论】: