Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

asp.net mvc - Azure storage emulator - blob creation gives 403 Forbidden message

I am attempting to use the Azure Storage Emulator to work with blob storage. I just cannot seem to get it to work and have wasted an entire day trying without success. I keep getting a 403 forbidden error even though this is developer storage. I am hoping someone here can assist me.

I had previously successfully run this same program on Azure. However my 3 month trial expired without me really using it at all, and now that I've returned to looking at Azure I must run on storage emulator (after changing the connection string to dev storage).

I will describe in detail what I have done.

First let me reassure you that I have everything necessary installed (I think):

I have both Visual Studio 2012 Pro and Visual Studio 2012 Express for Web (which is free). I have installed the additional stuff required for Azure using Web Platform installer. If I look in Add/Remove programs I see I have

  • Windows Azure Tools for Microsoft Visual Studio 2012 -v2.1
  • Windows Azure Libraries for .NET -v2.1
  • Windows Azure Emulator -v2.1
  • Windows Azure Authoring Tools -v2.1

I am able to create a cloud project in VS2012 if I want to, and if I do it successfully launches on the emulator. So it seems my problem is only with emulator storage.

Here is what I have done:

Step 1.

I create a new ASP.NET MVC4 project. I'm using .NET framework 4.5. That creates the basic project template with a HomeController.cs and AccountController.cs

Step 2.

I use NuGet to get "Windows Azure Storage". I believe this puts WindowsAzure.Storage.dll version 2.0.6.1 in my project references. Aside from that I see that my references also include -Microsoft.WindowsAzure.Configuration v2.0.0.0 -Microsoft.WindowsAzure.Diagnostics v2.1.0.0 -Microsoft.WindowsAzure.ServiceRuntime v2.1.0.0 -Microsoft.WindowsAzure.StorageClient v1.7.0.0

Step 3.

In Web.config file, I add the following tag within the tags

This is basically the account name and key everyone is supposed to use when using the emulator.

Step 4.

In HomeController.cs I create an Action. This action is supposed to create a container on Azure blob storage, and upload a file to it. Here is the code. As you can see it is pretty much the standard code you find in beginner examples


    public ActionResult AddBlobToBlobContainerStorageEmulator()
    {
        // Retrieve storage account from connection string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["EmulatorStorageConnectionString"]);

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to the previously created container. 
        CloudBlobContainer container = blobClient.GetContainerReference("myemulatortcont");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();


        // Retrieve reference to a blob named "mytestblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference("myemulatortestblob.jpg");

        // Create or overwrite the "mytestblob" blob with contents from a local file.
        using (var fileStream = System.IO.File.OpenRead(@"E:TUTORIALSWindows AzureAzure NOTES and stuffable.jpg"))
        {
            blockBlob.UploadFromStream(fileStream);
        }

        return Content("Blob uploaded to container on storage emulator");
    }

Step 5.

I make sure the compute emulator is started : Start > All Programs > Windows Azure > Emulator > Windows Azure Compute Emulator

Step 6.

That's all. I did not create a cloud project or anything, so when I run this it will run on "Local IIS Web Server" as per the project properties (also known as IIS Express).

Now I go to Debug > Start Debugging, and it launches the site in a browser window as expected at localhost:57810.

If I navigate to

http://localhost:57810/Home/AddBlobToBlobContainerStorageEmulator

it should fire my Action method.

Instead I see

enter image description here

Line 118 is in RED.... so basically the container cannot be created.

Can someone can tell me what is wrong here. Do I need to somehow add some permission to the development storage emulator? I don't understand why it says Forbidden.

I was wondering if maybe there is some issue on my machine, or maybe there is some conflict between the Azure. Are the versions of the dlls in my project somehow incorrect for development emulator? Or maybe there is a bug in v2.1?

I've explained exactly how I made my project but I'd be willing to upload the entire thing if anyone is wants to just try running it.

Thank you for any help you can provide.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Quite frankly I am a little annoyed that the original code did not work. After all, the MSDN article by Microsoft (https://azure.microsoft.com/en-us/documentation/articles/storage-use-emulator/) clearly states we should use:

Account name: devstoreaccount1 Account key: Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==

So why the heck did that not work?

Basically storage emulator has different endpoints than the cloud storage account. For example, the default blob endpoint for a cloud storage account is http://[youraccount].blob.core.windows.net while the blob endpoint for storage emulator is http://127.0.0.1:10000. When you just specify the storage account name and key for the storage emulator in your connection string, storage client library treats it like a cloud storage account and tries to connect to http://devstoreaccount1.blob.core.windows.net using the account key you provided. Since the key for devstoreaccount1 in the cloud is not the one you provided, you get 403 error.

If you want to connect to storage emulator using account name and key, you would need to provide additional details like different endpoints. So your connection string would be something like:

var connectionString = @"DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;
    BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;
    TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;
    QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;";

Notice the endpoints I have specified in the connection string.

var connectionString = "UseDevelopmentStorage=true";

The code above is just the short form of the big connection string I specified above.

Hope this clarifies your doubt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...