Question

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!

Was it helpful?

Solution

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();

OTHER TIPS

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

On create set public access:

public async ValueTask<bool> CreateContainer(string name)
{
    try {
        await _blobServiceClient.CreateBlobContainerAsync(name, PublicAccessType.BlobContainer);
        return true;
    }
    catch { }
    return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top