【问题标题】:How to prevent large file POST requests using Azure APIM?如何使用 Azure APIM 防止大文件 POST 请求?
【发布时间】:2020-02-28 08:57:46
【问题描述】:

如何使用 Azure APIM 防止大文件请求?

示例:阻止文件大小 > 50MB 的任何 POST 请求

【问题讨论】:

标签: azure azure-api-management


【解决方案1】:

您可以对所有 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>

【讨论】:

    【解决方案2】:

    有称为 quota-by-Keyquota-by-subscription 的策略,这将帮助用户阻止超出指定带宽的呼叫。请验证此链接以获取更多详细信息。enter link description here

    【讨论】:

      猜你喜欢
      • 2021-01-15
      • 1970-01-01
      • 2014-06-17
      • 2015-04-17
      • 2020-09-22
      • 2017-08-29
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      相关资源
      最近更新 更多