How to map an Azure App Service Web App virtual directory to Azure Storage Container

OK, so I tricked you…you can’t do what is said in the title, well I better not say you can’t because someone could possibly come up with a a way, but what I should say is that if you try to create a virtual directory in the portal as shown in Figure 1, that points to an Azure Storage Container, you will get the following error:

Failed to update web app settings for map-vd-storage: {“Code”:”BadRequest”,”Message”:
“The provided directory https://******.blob.core.windows.net/ is not located within
site\\.”,”Target”:null,”Details”:[{“Message”:”The provided directory
https://*******.blob.core.windows.net/ is not located within site\\.”},
{“Code”:”BadRequest”},{“ErrorEntity”:{“ExtendedCode”:”04034″,”MessageTemplate”:
“The provided directory {0} is not located within {1}.”,”Parameters”:[
“https://*******.blob.core.windows.net/”,”site\\”],”Code”:”BadRequest”,”Message”:
“The provided directory https://*******.blob.core.windows.net/ is not located within
site\\.”}}],”Innererror”:null}

image

Figure 1, how to codeless point an Azure Web App to an Azure Storage Container

Instead of trying to do that, you might consider using a URL Rewrite rule as shown here.

<system.webServer>
     <rewrite>
       <rules>
         <rule name=”Redirect to storage”>
           <match url=”^images/.*”/>
           <action type=”Redirect” url=”https://***.blob.core.windows.net/{R:0}” redirectType=”Permanent” />
         </rule>
       </rules>
     </rewrite>
   </system.webServer>

The URL Rewrite rule is looking for any request that contains ^images/.* in it.  What does the ^ (caret) mean in the URL Rewrite rule?  It means “look at the beginning of the line” when using the caret ( ^ ) the expression needs to be found anywhere in the URL and the .* (dot star) matches zero or more of any character.  If a match is made, then the request is redirected to my Azure Storage container, for example a request made to:

http://?.azurewebsites.net/images/1.bmp would match the rule and the request would be redirected to https://?.blob.core.windows.net/images/1.bmp

The following HTML code, hosted on an Azure App Service Web App would be redirected to the Azure Storage Container:

<html>
<head>
     <title>How to redirect a request to an Azure Web App to an Azure Blob Storage Container</title>
</head>
<body>
     <img src=”https://******.azurewebsites.net/images/2.bmp” />
</body>
</html>

This approach does result in a 301, see Figure 2, I did not try to find any way around that, share if you know.  Otherwise, HTH!

image

Figure 2, how to redirect requests to an Azure Web App to Azure Storage