1

I am trying to bulk upload files via APIM to azure storage account. In local, I am able to upload the files to azure storage account using REST API. but, through APIM I am unable to upload the files. I am getting 401 error. can you show me which APIM policy I should use to upload the file via APIM.

threeleggedrabbit
  • 1,722
  • 2
  • 28
  • 60

1 Answers1

0

Thanks to @Prashant Kumar for the blog APIM Policy to perform Azure Storage File Shares operations.

Below are the steps followed through his document,

  • Created API management services and Azure storage account.
  • Then for sample purpose selected Echo API from All API's then from design tab in All operations selected post Create resource and here get 200 ok response.
  • Then as shown in below image added below policy in inbound processing policy by clicking edit policy icon and click on save. Here is the sample send request policy used in between and as shown in image.
 <!-- Initialize context variables with property values. -->

        <set-variable name="storageAccount" value="NameOfYourStorageAccount" />

        <set-variable name="x-request-body" value="@(context.Request.Body.As<string>())" />

        <set-variable name="x-request-body-length" value="@{

 return (string)context.Request.Headers.GetValueOrDefault("Content-Length","0");

}" />

  <send-request mode="new" response-variable-name="tokenstate" timeout="2" ignore-error="true">

            <set-url>@{

 return string.Format("https://{0}.file.core.windows.net/NamOfYourFileShares/FileNamewithextension?SASToken ",(string)context.Variables["storageAccount"]);

 }</set-url>

            <set-method>PUT</set-method>

            <set-header name="x-ms-type" exists-action="override">

                <value>file</value>

            </set-header>

            <set-header name="x-ms-file-permission" exists-action="override">

                <value>inherit</value>

            </set-header>

            <set-header name="x-ms-file-attributes" exists-action="override">

                <value>none</value>

            </set-header>

            <set-header name="x-ms-file-creation-time" exists-action="override">

                <value>now</value>

            </set-header>

            <set-header name="x-ms-file-last-write-time" exists-action="override">

                <value>now</value>

            </set-header>

            <set-header name="x-ms-content-length" exists-action="override">

                <value>65336</value>

            </set-header>

        </send-request>

In the above send request change storage account, container/filename and SAS token details with yours. enter image description here

  • Then Go to Test tab of same policy and select the updated resource as shown in below images. enter image description here
  • You will get 200 ok response status as shown. enter image description here
  • Able to retrieve files in azure storage. If you want to retrieve files in blob storage, then in above policy change storage details as per requirement. enter image description here

but from the APIM I am unable to upload the file as it saying 401 error

Note:When a file is uploaded to an Azure Storage Account using APIM, a 401 error is returned, signifying that access to the storage account is not permitted. This can be because the SAS token or the storage account key, which are authentication credentials, are missing or wrong.

  • You should verify that the authentication credentials provided in the request are accurate and have the necessary access rights to the storage account in order to fix this problem. To authenticate and authorise the request, you can create an SAS token for a particular container or blob in your storage account and include it in the request headers.

  • You can also refer these MS document and SO question.

vijaya
  • 1,525
  • 1
  • 2
  • 6