Wednesday, 31 December 2014

How to download Blobs from Windows Azure

In this Post i would like tto explain about Downloading of Blobs using C# Code.

There are so many ways to download the Blob, am going to Explain Using Shared access signature(SAS).



//Using Azure Storage
// Retrieve storage account from connection string.           
 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
                    // Create the blob client.
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                    string containerName = (sender as LinkButton).CommandArgument;
                    string fileName = (sender as LinkButton).Text;
                    //getContainer Name
                    CloudBlobContainer container = blobClient.GetContainerReference("myContainer");
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

                    if (blockBlob.Exists())
                    {
                        //set the cache control
                        Response.Cache.SetCacheability(HttpCacheability.Public);
                        Response.Cache.SetValidUntilExpires(true);
                        Response.Cache.SetMaxAge(TimeSpan.FromSeconds(300));
                        Response.Expires = 300;
                       
                        //set the output content type
                        Response.ContentType = blockBlob.Properties.ContentType;
                        Response.AppendHeader("Content-Disposition", "attachment; filename="+fileName);

                        //download the asset to the output stream
                        blockBlob.DownloadToStream(Response.OutputStream);
                    }
 



No comments:

Post a Comment