在 Azure 逻辑应用中使用分块处理大型消息Handle large messages with chunking in Azure Logic Apps

处理消息时,逻辑应用通过最大大小来限制消息内容。When handling messages, Logic Apps limits message content to a maximum size. 此限制可以减少大型消息的存储和处理带来的开销。This limit helps reduce overhead created by storing and processing large messages. 逻辑应用可以通过分块将大消息拆分成小消息,以便处理其大小超出此限制的消息。To handle messages larger than this limit, Logic Apps can chunk a large message into smaller messages. 有了这种方式,就可以使用逻辑应用在特定条件下传输大文件。That way, you can still transfer large files using Logic Apps under specific conditions. 通过连接器或 HTTP 与其他服务通信时,逻辑应用可以使用大型消息,但只能采用区块的方式。When communicating with other services through connectors or HTTP, Logic Apps can consume large messages but only in chunks. 这种情况意味着,连接器也必须支持分块,或者逻辑应用和这些服务之间的基础 HTTP 消息交换必须使用分块。This condition means connectors must also support chunking, or the underlying HTTP message exchange between Logic Apps and these services must use chunking.

本文展示了如何为处理超过限制的消息的操作设置分块。This article shows how you can set up chunking for actions handling messages that are larger than the limit. 逻辑应用触发器不支持分块,因为交换多个消息的开销会增加。Logic App triggers don't support chunking because of the increased overhead of exchanging multiple messages.

哪些因素导致消息成为“大型”消息?What makes messages "large"?

消息是否为“大型”消息取决于处理这些消息的服务。Messages are "large" based on the service handling those messages. 不同的逻辑应用和连接器对大型消息的具体大小限制并不相同。The exact size limit on large messages differs across Logic Apps and connectors. 逻辑应用和连接器都不能直接使用大型消息,必须将这些消息分块。Both Logic Apps and connectors can't directly consume large messages, which must be chunked. 有关逻辑应用消息的大小限制,请参阅逻辑应用限制和配置For the Logic Apps message size limit, see Logic Apps limits and configuration. 有关每个连接器的消息大小限制,请参阅连接器的特定技术详细信息For each connector's message size limit, see the connector's specific technical details.

逻辑应用的分块消息处理Chunked message handling for Logic Apps

逻辑应用不能直接使用大于消息大小限制的分块消息输出。Logic Apps can't directly use outputs from chunked messages that are larger than the message size limit. 只有支持分块的操作才能访问这些输出中的消息内容。Only actions that support chunking can access the message content in these outputs. 因此,负责处理大型消息的操作必须满足以下条件之一:So, an action that handles large messages must meet either these criteria:

  • 在该操作属于某个连接器时能够以本机方式支持分块。Natively support chunking when that action belongs to a connector.
  • 在该操作的运行时配置中启用分块支持。Have chunking support enabled in that action's runtime configuration.

否则,在尝试访问大型内容输出时,会出现运行时错误。Otherwise, you get a runtime error when you try to access large content output. 若要启用分块,请参阅设置分块支持To enable chunking, see Set up chunking support.

连接器的分块消息处理Chunked message handling for connectors

与逻辑应用通信的服务可以有其自己的消息大小限制。Services that communicate with Logic Apps can have their own message size limits. 这些限制通常小于逻辑应用限制。These limits are often smaller than the Logic Apps limit. 例如,假定某个连接器支持分块,该连接器可能会将 30 MB 的消息视为大型消息,而逻辑应用则不会。For example, assuming that a connector supports chunking, a connector might consider a 30-MB message as large, while Logic Apps does not. 为了与此连接器的限制保持一致,逻辑应用会将任何大于 30 MB 的消息拆分成更小型的区块。To comply with this connector's limit, Logic Apps splits any message larger than 30 MB into smaller chunks.

支持分块的连接器的基础分块协议对最终用户不可见。For connectors that support chunking, the underlying chunking protocol is invisible to end users. 但是,并非所有连接器都支持分块,因此当传入消息超出连接器的大小限制时,这些不支持分块的连接器就会生成运行时错误。However, not all connectors support chunking, so these connectors generate runtime errors when incoming messages exceed the connectors' size limits.

备注

对于使用分块的操作,无法在这些操作中传递触发器正文或使用表达式(如 @triggerBody()?['Content'])。For actions that use chunking, you can't pass the trigger body or use expressions such as @triggerBody()?['Content'] in those actions. 相反,对于文本或 JSON 文件内容,可以尝试使用撰写操作创建变量来处理该内容。Instead, for text or JSON file content, you can try using the Compose action or create a variable to handle that content. 如果触发器正文包含其他内容类型(如媒体文件),则需要执行其他步骤来处理该内容。If the trigger body contains other content types, such as media files, you need to perform other steps to handle that content.

设置基于 HTTP 的分块Set up chunking over HTTP

在泛型 HTTP 方案中,可以拆分通过 HTTP 进行的大型下载和上传的内容,因此逻辑应用和终结点可以交换大型消息。In generic HTTP scenarios, you can split up large content downloads and uploads over HTTP, so that your logic app and an endpoint can exchange large messages. 但是,必须按逻辑应用预期的方式进行消息分块。However, you must chunk messages in the way that Logic Apps expects.

如果某个终结点为下载内容或上传内容启用了分块功能,则逻辑应用中的 HTTP 操作会自动将大型消息分块。If an endpoint has enabled chunking for downloads or uploads, the HTTP actions in your logic app automatically chunk large messages. 否则,必须在终结点上设置分块支持。Otherwise, you must set up chunking support on the endpoint. 如果没有终结点或连接器的所有权或控制权,则可能无法设置分块。If you don't own or control the endpoint or connector, you might not have the option to set up chunking.

另外,如果某个 HTTP 操作尚未启用分块功能,则还必须在操作的 runTimeConfiguration 属性中设置分块。Also, if an HTTP action doesn't already enable chunking, you must also set up chunking in the action's runTimeConfiguration property. 可以在操作中设置此属性,可以按照后面的说明在代码视图编辑器中直接进行,也可以按下述说明在逻辑应用设计器中进行:You can set this property inside the action, either directly in the code view editor as described later, or in the Logic Apps Designer as described here:

  1. 在 HTTP 操作的右上角选择省略号按钮 ( ... ),然后选择“设置”。In the HTTP action's upper-right corner, choose the ellipsis button (...), and then choose Settings.

    在操作中打开设置菜单

  2. 在“内容传输”下将“允许分块”设置为“启用”。 Under Content Transfer, set Allow chunking to On.

    启用分块

  3. 若要继续为下载或上传设置分块,请继续完成以下部分。To continue setting up chunking for downloads or uploads, continue with the following sections.

以区块形式下载内容Download content in chunks

在通过 HTTP GET 请求下载大型消息时,许多终结点会自动将这些消息以区块的形式发送。Many endpoints automatically send large messages in chunks when downloaded through an HTTP GET request. 通过 HTTP 从终结点下载分块的消息时,终结点必须支持部分内容请求,也称“分块下载”。To download chunked messages from an endpoint over HTTP, the endpoint must support partial content requests, or chunked downloads. 当逻辑应用向某个用于下载内容的终结点发送 HTTP GET 请求时,如果该终结点以“206”状态代码进行响应,则说明响应包含分块内容。When your logic app sends an HTTP GET request to an endpoint for downloading content, and the endpoint responds with a "206" status code, the response contains chunked content. 某个终结点是否支持部分请求不受逻辑应用的控制。Logic Apps can't control whether an endpoint supports partial requests. 但是,逻辑应用在获得第一个“206”响应时,会自动发送多个要求下载所有内容的请求。However, when your logic app gets the first "206" response, your logic app automatically sends multiple requests to download all the content.

若要检查终结点是否能够支持部分内容,请发送 HEAD 请求。To check whether an endpoint can support partial content, send a HEAD request. 此请求用于确定响应是否包含 Accept-Ranges 标头。This request helps you determine whether the response contains the Accept-Ranges header. 这样一来,如果终结点支持分块下载但没有发送分块内容,则可在 HTTP GET 请求中设置 Range 标头,以便建议此选项。That way, if the endpoint supports chunked downloads but doesn't send chunked content, you can suggest this option by setting the Range header in your HTTP GET request.

以下步骤说明了逻辑应用用来将分块内容从终结点下载到你的逻辑应用的详细过程:These steps describe the detailed process Logic Apps uses for downloading chunked content from an endpoint to your logic app:

  1. 你的逻辑应用向终结点发送 HTTP GET 请求。Your logic app sends an HTTP GET request to the endpoint.

    请求标头可以选择性地包括一个 Range 字段,用于描述请求内容区块的字节范围。The request header can optionally include a Range field that describes a byte range for requesting content chunks.

  2. 终结点使用“206”状态代码和 HTTP 消息正文进行响应。The endpoint responds with the "206" status code and an HTTP message body.

    有关此区块中内容的详细信息显示在响应的 Content-Range 标头中,其中包括有助于逻辑应用确定区块的开头和结尾的信息,以及整个内容在分块前的总大小。Details about the content in this chunk appear in the response's Content-Range header, including information that helps Logic Apps determine the start and end for the chunk, plus the total size of the entire content before chunking.

  3. 逻辑应用自动发送后续 HTTP GET 请求。Your logic app automatically sends follow-up HTTP GET requests.

    逻辑应用会一直发送后续 GET 请求,直至检索完整个内容。Your logic app sends follow-up GET requests until the entire content is retrieved.

例如,此操作定义显示一个设置 Range 标头的 HTTP GET 请求。For example, this action definition shows an HTTP GET request that sets the Range header. 此标头建议终结点使用分块内容进行响应:The header suggests that the endpoint should respond with chunked content:

"getAction": {
    "inputs": {
        "headers": {
            "Range": "bytes=0-1023"
        },
       "method": "GET",
       "uri": "http://myAPIendpoint/api/downloadContent"
    },
    "runAfter": {},
    "type": "Http"
}

GET 请求将表示字节范围的 "Range" 标头设置为 "bytes=0-1023"。The GET request sets the "Range" header to "bytes=0-1023", which is the range of bytes. 如果终结点支持部分内容请求,则终结点会使用所请求范围中的内容区块进行响应。If the endpoint supports requests for partial content, the endpoint responds with a content chunk from the requested range. "Range" 标头字段的确切格式因终结点而异。Based on the endpoint, the exact format for the "Range" header field can differ.

以区块形式上传内容Upload content in chunks

若要从 HTTP 操作上传分块内容,该操作必须已经通过操作的 runtimeConfiguration 属性启用了分块支持。To upload chunked content from an HTTP action, the action must have enabled chunking support through the action's runtimeConfiguration property. 此设置允许操作启动分块协议。This setting permits the action to start the chunking protocol. 然后,逻辑应用即可向目标终结点发送初始的 POST 或 PUT 消息。Your logic app can then send an initial POST or PUT message to the target endpoint. 在终结点以建议的区块大小进行响应以后,逻辑应用就会跟进,发送包含内容区块的 HTTP PATCH 请求。After the endpoint responds with a suggested chunk size, your logic app follows up by sending HTTP PATCH requests that contain the content chunks.

以下步骤说明了逻辑应用用来将分块内容从你的逻辑应用上传到终结点的详细过程:These steps describe the detailed process Logic Apps uses for uploading chunked content from your logic app to an endpoint:

  1. 你的逻辑应用使用空的消息正文发送初始的 HTTP POST 或 PUT 请求。Your logic app sends an initial HTTP POST or PUT request with an empty message body. 请求标头包括与逻辑应用需要以区块形式上传的内容的以下信息:The request header, includes this information about the content that your logic app wants to upload in chunks:

    逻辑应用请求标头字段Logic Apps request header field ValueValue 类型Type 说明Description
    x-ms-transfer-modex-ms-transfer-mode 分块chunked StringString 指示内容以区块形式上传Indicates that the content is uploaded in chunks
    x-ms-content-lengthx-ms-content-length <content-length><content-length> IntegerInteger 整个内容在分块之前的大小(以字节为单位)The entire content size in bytes before chunking
  2. 终结点以“200”成功状态代码和以下可选信息进行响应:The endpoint responds with "200" success status code and this optional information:

    终结点响应标头字段Endpoint response header field 类型Type 必须Required 说明Description
    x-ms-chunk-sizex-ms-chunk-size IntegerInteger No 建议的区块大小(以字节为单位)The suggested chunk size in bytes
    LocationLocation StringString Yes 要向其发送 HTTP PATCH 消息的 URL 位置The URL location where to send the HTTP PATCH messages
  3. 逻辑应用创建并发送后续 HTTP PATCH 消息 - 每条消息包含以下信息:Your logic app creates and sends follow-up HTTP PATCH messages - each with this information:

    • 基于 x-ms-chunk-size 或某个内部计算大小的内容区块,直至顺序上传总计 x-ms-content-length 的所有内容A content chunk based on x-ms-chunk-size or some internally calculated size until all the content totaling x-ms-content-length is sequentially uploaded

    • 这些标头详述了在每个 PATCH 消息中发送的内容区块:These header details about the content chunk sent in each PATCH message:

      逻辑应用请求标头字段Logic Apps request header field ValueValue 类型Type 说明Description
      Content-RangeContent-Range <range><range> StringString 当前内容区块的字节范围,包括起始值、结束值、内容总大小,例如:"bytes=0-1023/10100"The byte range for the current content chunk, including the starting value, ending value, and the total content size, for example: "bytes=0-1023/10100"
      Content-TypeContent-Type <content-type><content-type> StringString 分块内容的类型The type of chunked content
      Content-LengthContent-Length <content-length><content-length> StringString 当前区块的大小长度(以字节为单位)The length of size in bytes of the current chunk
  4. 每次进行 PATCH 请求之后,终结点会以“200”状态代码和以下响应标头进行响应,以此确认每个区块的接收情况:After each PATCH request, the endpoint confirms the receipt for each chunk by responding with the "200" status code and the following response headers:

    终结点响应标头字段Endpoint response header field 类型Type 必须Required 说明Description
    范围Range StringString Yes 终结点收到的内容的字节范围,例如:“bytes=0-1023”The byte range for content that has been received by the endpoint, for example: "bytes=0-1023"
    x-ms-chunk-sizex-ms-chunk-size IntegerInteger No 建议的区块大小(以字节为单位)The suggested chunk size in bytes

例如,此操作定义显示一个要求将分块内容上传到终结点的 HTTP POST 请求。For example, this action definition shows an HTTP POST request for uploading chunked content to an endpoint. 在操作的 runTimeConfiguration 属性中,contentTransfer 属性将 transferMode 设置为 chunkedIn the action's runTimeConfiguration property, the contentTransfer property sets transferMode to chunked:

"postAction": {
    "runtimeConfiguration": {
        "contentTransfer": {
            "transferMode": "chunked"
        }
    },
    "inputs": {
        "method": "POST",
        "uri": "http://myAPIendpoint/api/action",
        "body": "@body('getAction')"
    },
    "runAfter": {
    "getAction": ["Succeeded"]
    },
    "type": "Http"
}