Read text file from Bolb storage using StreamReader object RRS feed

  • Question

  • Hi,

    I have uploaded textfile into blob and now I want to read same file  into StreamReader  object ...Its build successfully but when keep loop to read file using Peek() method , its going in infinite loop.

    I used below code :

    Stream stream = null;
                    WebClient webClient = new WebClient();
                    stream = webClient.OpenRead("blobURI");
                    string content = null;

                    StreamReader sr = new StreamReader(stream);
                    content = sr.ReadToEnd();
                    StreamReader ms = new StreamReader(new MemoryStream(System.Text.Encoding.ASCII.GetBytes(content.Trim())));
                    //Read each row from the data file
                   string strFeedHeader = content; 

                   string[] strHeader = strFeedHeader.Split(this._delimeter.ToCharArray());
                    Config.FileHeader = strFeedHeader;
                    CSVReader objCSVReader = new CSVReader();
                    string[] strData;
                    int recordCount = 0;
                    ms.BaseStream.Position = 0;
                    while (ms.Peek() > -1) {   }


    Any idea to do same thing in other way?

    Thanks in advance..



    Monday, October 25, 2010 1:33 PM


  • The first step in diagnosing any problem with Azure Storage should always be to use Fiddler to identify precisely what is sent over the wire to Azure Storage. An issue with the code may be the use of WebClient instead of CloudBlobClient since WebClient does not handle the authentication (probably) required by Azure Storage. I use probably since it would not be required were the container to have public access. You can use a CloudBlobClient as follows:

    static public void DownloadToMemoryStream(CloudBlobClient cloudBlobClient, String containerName, String blobName)
    	CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
    	CloudBlob cloudBlob = cloudBlobContainer.GetBlobReference(blobName);
    	MemoryStream memoryStream = new MemoryStream();
    	memoryStream.Position = 0;
    	StreamReader streamReader = new StreamReader(memoryStream);
    	String blobText = streamReader.ReadToEnd();

    Note that the following is documented for StreamReader.Peek():

    The current position of the StreamReader object is not changed by Peek.

    This is probably pertinent regarding the infinite-loop problem.

    • Proposed as answer by Patriek van Dorp Tuesday, October 26, 2010 3:54 AM
    • Marked as answer by C.P.Singh Tuesday, October 26, 2010 3:58 AM
    Monday, October 25, 2010 4:55 PM