使用 multipart/form-data 将大文件从 Azure APIM 上传到 Azure Blob 存储

问题描述 投票:0回答:1

我在 Azure APIM 中使用以下策略将大文件 (2 GB) 上传到 Azure Blob 存储。当我将其作为二进制(即内容类型“application/octet-stream”)发送时,效果很好。但是,我需要以“multipart/form-data”的内容类型上传它,因为它是一个大文件,但无法这样做,因为 APIM 不支持 context.Request.File。 谁能帮助我使用 APIM 策略代码来解析多部分表单数据?

<policies>
<inbound>
<base />
<!-- Step 1: Set backend service to Azure Blob Storage -->
<set-backend-service base-url="@{ return "https://{{storage account}}.blob.core.windows.net/" ;}" />
<rewrite-uri template="@{ return  context.Request.Url.Query.GetValueOrDefault("blobContainer", "test") + "/" + 
                context.Request.Url.Query.GetValueOrDefault("fileNameWithExtension", "test.mp4");}" />
<set-method>PUT</set-method>
<set-header name="Host" exists-action="override">
<value>sacuksnprdiidlrawast6001.blob.core.windows.net</value>
</set-header>
<set-header name="X-Ms-Blob-Type" exists-action="override">
<value>BlockBlob</value>
</set-header>
<set-header name="X-Ms-Version" exists-action="override">
<value>2019-12-12</value>
</set-header>
<set-header name="Accept" exists-action="override">
<value>application/json</value>
</set-header>
<authentication-managed-identity resource="https://storage.azure.com/" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
azure azure-blob-storage multipartform-data azure-api-management apim
1个回答
0
投票

当我将其作为二进制(即内容类型“application/octet-stream”)发送时,效果很好。但是,我需要以“multipart/form-data”的内容类型上传它,因为它是一个大文件。

根据 Gaurav Mantri 的SO-Answer。 Azure Blob 存储不支持

multipart/form-data
内容类型。

如果您想上传较大的文件,可以使用

application/octet-stream
内容类型。另一方面,如果您想上传图像或视频,您可以使用
content-type=image/jpeg
video/mp4 
内容类型。

在下面的策略中,我添加了 content-Type 参数来设置文件的内容类型(视频/mp4)。

政策:

<policies>
<inbound>
<base />
<!-- Step 1: Set backend service to Azure Blob Storage -->
<set-backend-service base-url="@{ return "https://{{storage account}}.blob.core.windows.net/" ;}" />
<rewrite-uri template="@{ return  context.Request.Url.Query.GetValueOrDefault("blobContainer", "test") + "/" + 
                context.Request.Url.Query.GetValueOrDefault("fileNameWithExtension", "sample.mp4");}" />
<set-method>PUT</set-method>
<set-header name="Host" exists-action="override">
<value>venkat6781.blob.core.windows.net</value>
</set-header>
<set-header name="X-Ms-Blob-Type" exists-action="override">
<value>BlockBlob</value>
</set-header>
<set-header name="X-Ms-Version" exists-action="override">
<value>2019-12-12</value>
<set-header name="Content-Type" exists-action="override">
<value>video/mp4</value>
</set-header>
<set-header name="Accept" exists-action="override">
<value>application/json</value>
</set-header>
<authentication-managed-identity resource="https://storage.azure.com/" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>

输出:

enter image description here

参考: Put Blob (REST API) - Azure 存储 |微软学习

© www.soinside.com 2019 - 2024. All rights reserved.