简体   繁体   English

将Azure Blob与Azure网站连接

[英]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). 我正在尝试将Azure网站连接到Azure blob(我打算在容器中托管一些文件,然后从我的网站上获取它们)。

I started out with this tutorial: http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/ 我从这个教程开始: 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 我部署了我的网站,然后开始学习本教程: 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). 由于我使用的是Azure网站,因此我将以下代码添加到了我的web.config文件中(在WebApplication1项目下)。 There is also a web.config file under the Views folder but I didn't modify that. Views文件夹下还有一个web.config文件,但我没有修改它。

 <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: 我按照教程中的所有步骤安装了相关的NuGet包,然后将这些命名空间包含在我的Controllers/HomeController.cs文件中:

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). 然后我在ActionResult Index()方法中添加了以下语句(默认情况下运行)。

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

When I try and run the solution, I now get this error: 当我尝试运行解决方案时,我现在收到此错误:

在此输入图像描述

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: 我还尝试直接放入StorageConnectionString的值(使用我的帐户名和密钥),而不是在以下语句中引用它:

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

You have a fundamental mistake in your code. 您的代码中存在根本性错误。

First you set an AppSetting: 首先设置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. 设置AppSetting时,您需要阅读AppSetting。 When you set ConnectionString, you need to read Connection String. 设置ConnectionString时,需要读取连接字符串。

So, the solution to be just keep the web.config as is, and change the line where you get storage account to: 因此,解决方案只是将web.config保持原样,并将存储帐户所在的行更改为:

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

Or keep your line for Connection strings but change web.config to: 或者保留连接字符串的行,但将web.config更改为:

 <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). 当然,您必须为云存储帐户和存储帐户密钥设置您的真实值( account-name将永远不会起作用)。

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. 这是来自Azure的更糟糕的文档,文章确实告诉您创建AppSetting然后代码告诉您检索ConnectionString。

The alternative fix is to store the details as a ConnectionString and leave the code as is: 另一种解决方法是将详细信息存储为ConnectionString,并保留代码:

<add name="StorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=your-account;AccountKey=your-key" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM