Wednesday, September 30, 2015

Reading data from Azure Storage Blob into a string

The code below I used to read the text from the file located on Azure blob into a string:
string filename ="mytargetfile.txt"
string blobConn = CloudConfigurationManager.GetSetting("BlobConn");
CloudStorageAccount storageAcct = CloudStorageAccount.Parse(blobConn);
CloudBlobClient blobClient = storageAcct.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("container1");
if (blobContainer.Exists(null, null))
{
    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(filename);
    if (blockBlob.Exists(null, null))
    {
         //read data from the blob into a stream and get it into an array
         using (var ms = new MemoryStream())
        {
             blockBlob.DownloadToStream(ms);
             ms.Position = 0;
             using (var reader = new StreamReader(ms, Encoding.Unicode))
             {
                   var fileText = reader.ReadToEnd();
             }
        }
    }
}




Tuesday, September 29, 2015

Saving file from Azure Storage Blob onto a hard drive using C#

Recently started working with Azure storage blob. So far I am really enjoying it so I wanted to share some code snippet I have wrote so far for various purposes. The one below I have used to save the file from the blob onto my hard drive.
//reading blob file data into a file on your hard drive
string filename ="mytargetfile.txt"
string blobConn = CloudConfigurationManager.GetSetting("BlobConn");
CloudStorageAccount storageAcct = CloudStorageAccount.Parse(blobConn);
CloudBlobClient blobClient = storageAcct.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("container1");
if (blobContainer.Exists(null, null))
{
     CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(filename);
     if (blockBlob.Exists(null, null))
    {
          string tempFile = Path.Combine(@"C:\BlobFiles",filename);
          blockBlob.DownloadToFile(tempFile, FileMode.Create);
    }
}

Wednesday, September 2, 2015

How to loop through multiple cookies from an HttpWebResponse

Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
request.CookieContainer = New CookieContainer()
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

' Loop through the cookie collection and display each property 
Dim _cookie As Cookie
For Each _cookie In  response.Cookies
     Console.WriteLine("Cookie:")
     Console.WriteLine("{0} = {1}", _cookie.Name, _cookie.Value)
     Console.WriteLine("Domain: {0}", _cookie.Domain)
     Console.WriteLine("Path: {0}", _cookie.Path)
     Console.WriteLine("Port: {0}", _cookie.Port)
     Console.WriteLine("Secure: {0}", _cookie.Secure)

     Console.WriteLine("When issued: {0}", _cookie.TimeStamp)
     Console.WriteLine("Expires: {0} (expired? {1})", _cookie.Expires, _cookie.Expired)
     Console.WriteLine("Don't save: {0}", _cookie.Discard)
     Console.WriteLine("Comment: {0}", _cookie.Comment)
     Console.WriteLine("Uri for comments: {0}", _cookie.CommentUri)
     Console.WriteLine("Version: RFC {0}", IIf(_cookie.Version = 1, "2109", "2965"))

     ' Show the string representation of the cookie.
     Console.WriteLine("String: {0}", _cookie.ToString())

     ' Show sessionId of the cookie.
     Console.WriteLine("SessionId: {0}", _cookie.Value)
Next _cookie