Thursday, 25 August 2016

Download blob from Azure storage

Technology : Azure, Azure Storage

Name Spaces Required:

using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage;

       

        public async Task<string> GetBlobData()
        {
            string AccountName = "Azure Storage Name";
            string AccountKey = "Azure storage Key";
            string ContainerName = "Container name";
            //StorageCredentials credentials = new StorageCredentials(AccountName, AccountKey);

            string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", AccountName, AccountKey);
            var blobClient = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(storageConnectionString).CreateCloudBlobClient();
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer _BlobContainer = blobClient.GetContainerReference(ContainerName);
            OperationContext context = new OperationContext();
            BlobRequestOptions options = new BlobRequestOptions();
            BlobResultSegment blobSeg = await _BlobContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, options, context);
            CloudBlockBlob result = null;
            foreach (CloudBlob tempObj in blobSeg.Results)
            {
                if (tempObj.Properties.LastModified == DateTime.Now)
                    result = (CloudBlockBlob)tempObj;
            }
            string resultContent =await result.DownloadTextAsync();
            //List<CloudBlob> lstBlobs= blobSeg.Results.Where(x=>x.)


            return resultContent;

        }

No comments:

Post a Comment