Using Azure Blobs with the Azure Website

I am making a Windows Azure MVC site that includes users uploading images. I wanted to save images into blobs. I've searched for tutorials, but most of them are about Webapps, not MVC websites.

The only useful tutorial I found was: http://www.codeproject.com/Articles/490178/How-to-Use-Azure-Blob-Storage-with-Azure-Web-Sites

I'm new to the whole MVC / Windows Azure / Visual Studio scene and I got confused in step 7 where I had to connect to a Cloud Storage account.

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

      

I cannot figure out where I should put this bit of code.

Same for the code in step 8: Create the container

blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("productimages");
if (container.CreateIfNotExist())
{
    // configure container for public access
    var permissions = container.GetPermissions();
    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
    container.SetPermissions(permissions);
}

      

And Step 9. Save the image to BLOB

string uniqueBlobName = string.Format("productimages/image_{0}{1}", 
   Guid.NewGuid().ToString(), Path.GetExtension(image.FileName));
CloudBlockBlob blob = blobStorage.GetBlockBlobReference(uniqueBlobName);
blob.Properties.ContentType = image.ContentType;
blob.UploadFromStream(image.InputStream);

      

Any help would be really appreciated. Any other links to related tutorials are also welcome.

Thank you so much!

+3


source to share


2 answers


I'm assuming you've added the Windows Azure Storage NuGet package to your web application project? It didn't seem explicit in the link you quoted, but if you didn't, you would see a bunch of unresolved compiler errors.

From a code point of view, it's more up to you. The purpose is the storageAccount

same as setting the database connection string; nothing happens through the wire, so you can install it once and make it available throughout your application as part of some static instance of the class.

As far as creating a container is concerned, you should do this every time you want to store data - think of it as creating a folder on the filesystem, or perhaps a table in a relational database. If your application always stores information in the same "hard-coded" container, you can pre-install your container in the portal and not code.



Entering code like this, although providing a little fault tolerance if the container has been removed through some external process; just like an application using SQL Server might check for the existence of the Employee table before doing updates / retrieval, as it is possible (but unlikely) that someone has dropped the Employee table through some other SQL tool.

Saving the image to blob will be placed directly behind any controller responsible for initiating this action. This is where you do "real" work. The code in your example specifically refers to the container productimages

, so it can be immediately preceded by the code in step 8 if you want to make sure that there is no way to delete the container (for example, via a portal). Again, this would be like checking for the existence of a database table in traditional client / server code every time the data in it is accessed (which most of us may consider overkill, and we resort to exception handling to cover this contingency).

+2


source


Old question, but noobs can still help!

Azure Webapps does accept MVC apps. Webapps on Azure is just a container that I'm pretty sure can accept different types of ASP.NET based applications, Forms or MVC, C # or VB.net.

Assuming you know the basics of an MVC application, you can simply create an Azure Webapp and publish it there. It's as straight forward as it is.

This code:

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

      

And this:



blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("productimages");
if (container.CreateIfNotExist())
{
    // configure container for public access
    var permissions = container.GetPermissions();
    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
    container.SetPermissions(permissions);
}

      

You can go into a new class specifically for Blob services, for example. name it something like blobServices.cs and save it in the root directory of your MVC application and it might look like this:

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

    blobStorage = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobStorage.GetContainerReference("productimages");
    if (container.CreateIfNotExist())
    {
        // configure container for public access
        var permissions = container.GetPermissions();
        permissions.PublicAccess = BlobContainerPublicAccessType.Container;
        container.SetPermissions(permissions);
    }

      

This is most likely best practice. From there, in the controller of your MVC application, you can do something like this:

namespace blabla.cs
{

   public controller myBlobController
    {

      //instantiate the blob service
      BlobProfileImageServices _blobServices = new BlobProfileImageServices();

       public public ActionResult Index()
        {



            //call your blob storage service
            //and loop through each blob
            CloudBlobContainer blobContainer = _blobServices.GetCloudBlobContainer();
            List<string> blobs = new List<string>();
            foreach (var blobItem in blobContainer.ListBlobs())
            {
                blobs.Add(blobItem.Uri.ToString());

            }


      }

    }
}

      

0


source







All Articles