Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Azure Blob with Azure Website

I'm trying to connect an Azure website to an Azure blob (where I intend to host some files in a container and then grab them from my website).

I started out with this tutorial: http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/

I deployed my website, and then started following this tutorial: http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#setup-connection-string

Since I was using an Azure website, I added the following code to my web.config file (under the WebApplication1 project). There is also a web.config file under the Views folder but I didn't modify that.

 <configuration>
    <appSettings>
       <add key="StorageConnectionString" 
            value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
    </appSettings>
 </configuration>

I followed all the steps in the tutorial and installed the relevant NuGet packages and then included these namespaces in my Controllers/HomeController.cs file:

using System.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

Then I added the following statement in the ActionResult Index() method (which runs by default).

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

When I try and run the solution, I now get this error:

enter image description here

I also tried directly putting the value of the StorageConnectionString (with my account name and key) instead of referencing to it in the following statement:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key"].ConnectionString);

I still get the same error. I can't seem to find anything on the internet. Any ideas?

Thanks!

like image 799
Order66 Avatar asked Sep 05 '14 06:09

Order66


People also ask

How do I access my Azure storage on the Web App?

In the Azure portal, go into your storage account to grant your web app access. Select Access control (IAM) in the left pane, and then select Role assignments. You'll see a list of who has access to the storage account.

How do I access blob from my browser?

If you are trying to access the blob you need to specify the container name and the blob name. Suppose, you have a blob by name "MyBlob" present in "mycontainer",you can retrieve it by specifying http://my_storageAcount.blob.core.windows.net/mycontainer/MyBlob.


2 Answers

You have a fundamental mistake in your code.

First you set an AppSetting:

 <configuration>
    <appSettings>
       <add key="StorageConnectionString" 
            value="DefaultEndpointsProtocol=https;AccountName=account-   name;AccountKey=account-key" />
    </appSettings>
 </configuration>

Then you try to get a connection string:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

This will simply not work. When set AppSetting, you need to read AppSetting. When you set ConnectionString, you need to read Connection String.

So, the solution to be just keep the web.config as is, and change the line where you get storage account to:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

Or keep your line for Connection strings but change web.config to:

 <configuration>
    <connectionStrings>
       <add name="StorageConnectionString" 
            connectionString="DefaultEndpointsProtocol=https;AccountName=account-   name;AccountKey=account-key" providerName="System.Data.SqlClient" />
    </connectionStrings>
 </configuration>

And of course, you have to put your real values for Cloud Storage account and Storage Account key (account-name will simply never work).

like image 182
astaykov Avatar answered Sep 19 '22 15:09

astaykov


This is more bad documentation from Azure, the article does indeed tell you to create an AppSetting and then the code tells you to retrieve a ConnectionString.

The alternative fix is to store the details as a ConnectionString and leave the code as is:

<add name="StorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=your-account;AccountKey=your-key" />
like image 42
Munes Avatar answered Sep 20 '22 15:09

Munes