Tuesday, 23 October 2018

Read DB data to create a file and store in the Azure Storage

Code to read DB data


        public async void getData()

        {

          var dbList = dbContext.TESTs.ToList();

            await generateFile(dbList);

        }



To Generate File and store in Azure Container


public async Task generateFile(List data)

        {

            string AccountName = "XXXXXXX";

            string AccountKey = "xxxxxxxx";

            string ContainerName = "xxxxxxxx";

           



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

            CloudBlockBlob result = null;



            // Create a file in your local MyDocuments folder to upload to a blob.

            string localPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            string localFileName = "QuickStart_" + Guid.NewGuid().ToString() + ".txt";

            var sourceFile = Path.Combine(localPath, localFileName);

            // Write text to the file.

            string dataString="";

            data.ForEach(x => { dataString = dataString + x.Fname; });

            File.WriteAllText(sourceFile, dataString);





            // Get a reference to the blob address, then upload the file to the blob.

            // Use the value of localFileName for the blob name.

            CloudBlockBlob cloudBlockBlob = _BlobContainer.GetBlockBlobReference(localFileName);

            cloudBlockBlob.UploadFromFile(sourceFile);



           

            

        }

No comments:

Post a Comment