Vivek Chandel
Vivek Chandel

Reputation: 1

Uploading large file using multipart/form-data from Azure APIM to Azure Blob Storage

I am using the below policy in Azure APIM for uploading large file (2 GB) to Azure Blob Storage. This works fine when I send it as binary (i.e. content type "application/octet-stream"). However, I need to upload it with content type as "multipart/form-data" as it's a large file, but unable to do so, as context.Request.File is not supported in APIM. Can anyone help me with the APIM policy code to parse the multipart form-data?

<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>

Upvotes: 0

Views: 330

Answers (1)

Venkatesan
Venkatesan

Reputation: 10455

This works fine when I send it as binary (i.e. content type "application/octet-stream"). However, I need to upload it with content type as "multipart/form-data" as it's a large file.

According to this SO-Answer by Gaurav Mantri. Azure blob storage doesn't support the multipart/form-data content type.

If you want to upload files that are larger in size, you can use the application/octet-stream content type. On the other hand, if you want to upload an image or video , you can use the content-type=image/jpeg or video/mp4 content type.

In below policy I added content-Type parameter to set the content type (video/mp4) for the file.

Policy:

<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>

Output:

enter image description here

Reference: Put Blob (REST API) - Azure Storage | Microsoft Learn

Upvotes: 0

Related Questions