Azure BlobStorageHelper – Blob create and upload

Code beautifier: http://hilite.me/

Code: 

namespace Helpers
{
    public sealed class BlobStorageHelper : StorageHelper
    {
        private readonly CloudBlobClient _blobClient;
        public string _containerName { get; set; }

        public BlobStorageHelper() : base()
        {
            _blobClient = base.StorageAccount.CreateCloudBlobClient();
            _containerName = ConfigurationManager.AppSettings["StorageContainerName"];
        }


        public Uri CreateBlob(Stream inputStream, string blobName)
        {
            CloudBlobContainer container = _blobClient.GetContainerReference(_containerName);
            container.CreateIfNotExists();

            ICloudBlob blob = container.GetBlockBlobReference(blobName);
            blob.UploadFromStream(inputStream);

            return blob.Uri;
        }

        public Uri CreateBlob(byte[] byteArray, string blobName)
        {
            CloudBlobContainer container = _blobClient.GetContainerReference(_containerName);
            container.CreateIfNotExists();

            ICloudBlob blob = container.GetBlockBlobReference(blobName);
            blob.UploadFromByteArray(byteArray, 0, byteArray.Length);

            return blob.Uri;
        }

        public string GetBlobFileDownloadPath(string blobPath)
        {
            string blobName = blobPath.Substring(blobPath.IndexOf("/" + _containerName + "/")).Replace("/" + _containerName + "/", "");

            CloudBlobContainer container = _blobClient.GetContainerReference(_containerName);
            container.CreateIfNotExists();

            ICloudBlob cloudBlob = container.GetBlobReferenceFromServer(blobName);

            var sasConstraints = new SharedAccessBlobPolicy();
            sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5);
            sasConstraints.Permissions = SharedAccessBlobPermissions.Read;

            var sasBlobToken = cloudBlob.GetSharedAccessSignature(sasConstraints);

            return blobPath + sasBlobToken;
        }

        public void DeleteBlob(string blobPath)
        {
            CloudBlobContainer container = _blobClient.GetContainerReference(_containerName);
            container.CreateIfNotExists();

            CloudBlob cloudBlob = container.GetBlockBlobReference(blobPath);

            cloudBlob.DeleteIfExists();
        }
    }
}

 

Azure BlobStorageHelper – Download file as byte array

Code beautifier: http://hilite.me/

Helper code: 

namespace Helpers
{
    public class BlobStorageHelper : StorageHelper
    {
        private readonly CloudBlobClient _blobClient;
        public string _containerName { get; set; }

        public BlobStorageHelper()
            : base()
        {
            _blobClient = base.StorageAccount.CreateCloudBlobClient();
            _containerName = ConfigurationManager.AppSettings["StorageContainerName"];
        }

        protected Stream DownloadBlobAsStream(string blobUri)
        {
            CloudStorageAccount account = this.StorageAccount;
            CloudBlockBlob blob = GetBlockBlobReference(account, blobUri);

            Stream mem = new MemoryStream();
            if (blob != null)
            {
                blob.DownloadToStream(mem);                
            }

            return mem;
        }

        private CloudBlockBlob GetBlockBlobReference(CloudStorageAccount account, string blobUri)
        {
            string blobName = blobUri.Substring(blobUri.IndexOf("/" + _containerName + "/")).Replace("/" + _containerName + "/", "");
            CloudBlobClient blobclient = account.CreateCloudBlobClient();
            CloudBlobContainer container = _blobClient.GetContainerReference(_containerName);
            container.CreateIfNotExists();
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
            return blob;
        }


        public byte[] DownloadBlobAsByeArray(string blobUri)
        {
            Stream inputStream = DownloadBlobAsStream(blobUri);

            byte[] buffer = new byte[16 * 1024];

            inputStream.Position = 0; // Add this line to set the input stream position to 0

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

       
    }


    public abstract class StorageHelper
    {
        protected readonly CloudStorageAccount StorageAccount;

        public StorageHelper()
        {
            this.StorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
        }
    }
}