1
votes

I am trying to read a csv file from my azure storage account. To convert each line into an object and build a list of those objects. It keeps erring, and the reason is it cant find the file (Blob not found). The file is there, It is a csv file.

File in azure storage

Error:

StorageException: The specified blob does not exist. BatlGroup.Site.Services.AzureStorageService.AzureFileMethods.ReadCsvFileFromBlobAsync(CloudBlobContainer container, string fileName) in AzureFileMethods.cs + await blob.DownloadToStreamAsync(memoryStream);

 public async Task<Stream> ReadCsvFileFromBlobAsync(CloudBlobContainer container, string fileName)
    {
        // Retrieve reference to a blob (fileName)
        var blob = container.GetBlockBlobReference(fileName);

        using (var memoryStream = new MemoryStream())
        {
            //downloads blob's content to a stream
             await blob.DownloadToStreamAsync(memoryStream);
            return memoryStream;

        }

    }

I've made sure the file is public. I can download any text file that is stored there, but none of the csv files.

I am also not sure what format to take it in as I need to iterate through the lines.

I see examples of bringing the whole file down to a temp drive and working with it there but that seems unproductive as then I could just store the file in wwroot folder instead of azure.

What is the most appropriate way to read a csv file from azure storage.

1
The most probable reason is that your path is wrong. If the file is in a virtual folder, you need to include the folder(s) into the file name, e.g. "folder/subfolder/file.jpg"juunas
The file path is correct, which is why I can read the text files.dinotom
That's extremely odd then, because the content type shouldn't affect your ability to download it.. It's just metadata.juunas
@juunas...and its not just that file, I can download all the txt files but none of the csv filesdinotom
“l” is lowercase when you’re passing in method while the blob name has it in uppercase.Gaurav Mantri

1 Answers

0
votes

Regarding how to iterate through the lines, after you get the memory stream, you can use StreamReader to read them line by line.

Sample code as below:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;

namespace ConsoleApp17
{
    class Program
    {
        static void Main(string[] args)
        {
            string connstr = "your connection string";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connstr);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("t11");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("students.csv");
            string text="";
            string temp = "";
            using (var memoryStream = new MemoryStream())
            {
                blockBlob.DownloadToStream(memoryStream);

                //remember set the position to 0
                memoryStream.Position = 0;
                using (var reader = new StreamReader(memoryStream))
                {
                    //read the csv file as per line.
                    while (!reader.EndOfStream && !string.IsNullOrEmpty(temp=reader.ReadLine()))
                    {
                        text = text + "***" + temp;
                    }

                }


            }

            Console.WriteLine(text);
            Console.WriteLine("-------");
            Console.ReadLine();
        }
    }
}

My csv file: enter image description here

The test result: enter image description here