Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot find uploaded image when using Azure Storage Emulator in local machine

asp.net 4.5, Web Forms, vs2013, Identity 2.0, Entity Framework 6.0

I plan to use Azure Storage blob to store user uploaded images. So I downloaded Azure Storage Emulator to test in my local machine.

It seems container is created correctly and image is saved correctly - since I didn't receive any exception when I run the application.

But the problem is that I cannot see this image on my web page. Debugging into the code, I saw this image's URI is

http://127.0.0.1:10000/devstoreaccount1/images/my_sub_folder_2/my_image.jpg

I paste this url to browser, IE tells me:

The webpage cannot be found 404

Chrome tells me:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>ResourceNotFound</Code>
<Message>
The specified resource does not exist. RequestId:e4b64fa8-5985-4aa4-bfbe-50aabd497537 Time:2014-04-21T07:26:04.4264011Z
</Message>
</Error>

I use AzureStorageAccess.CreateContainer() to create a container in the storage, and use AzureStorageAccess.UploadBlob() to save user uploaded image (stream) to the storage. The code is as follows.

public class AzureStorageAccess
{
// create a container in Azure Storage Emulator
    static public void CreateContainer(string containerName)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount
            .Parse("UseDevelopmentStorage=true");
        // CloudConfigurationManager.GetSetting("StorageConnectionString")
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        container.CreateIfNotExists();
    }

// upload a stream to Azure Storage Emulator
    static public void UploadBlob(System.IO.Stream stream, string containerName, string blobRelativeURI, out string blobURI)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount
            .Parse("UseDevelopmentStorage=true");
        // CloudConfigurationManager.GetSetting("StorageConnectionString")
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobRelativeURI);
        blockBlob.UploadFromStream(stream);
        blobURI = blockBlob.Uri.AbsoluteUri;
    }
}

I have this connection string in web.config. Initially I was using this connection string. Later when I am trying to solve the problem, I read some posts here in stackoverflow that I should use Parse("UseDevelopmentStorage=true"). You can see the code to read this connection string is commented out in above code.

<add key="StorageConnectionString"         value="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1"
/>  

Can anyone tell me what is the problem? Thank you very much!

like image 645
martial Avatar asked Apr 21 '14 07:04

martial


People also ask

Where does Azure storage emulator store files?

The Storage Emulator is installed by default to C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator .

How do I know if my Azure Blob Storage upload was successful?

Is there any way to check the status of a blob upload? If no exception throws from your code, it means the blob has been uploaded successfully. Otherwise, a exception will be thrown. You can also confirm it using any web debugging proxy tool(ex.

How do I access Azure Blob storage on Windows?

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

That is happening because containers by default turn off public read access. You will be able to access the blob from the browser if you change the Public Read Access property to Container using the Visual Studio Server Explorer

enter image description here

Or if you want to do this in code via the SDK add the following line before you create the container.

        //Create container
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
        container.CreateIfNotExists();
like image 122
Lukkha Coder Avatar answered Oct 16 '22 23:10

Lukkha Coder


Another way of doing it is directly trough Microsoft Azure Storage Explorer. Right click on your blob container and select Set Public Access Level...

Microsoft Azure Storage Explorer

like image 34
krempelj Avatar answered Oct 16 '22 22:10

krempelj