0

I'm trying to parse a .csv file located in Azure blob storage. When I parse the .csv file from my local using File.Readlines(), it works fine. When I pass the path of the blob and debug, I get the following message at File.Readlines():

enter image description here

Here's my code:

public IList<blabla> getFiles()
        {
            _rootFileDirectory = configurationService.GetString("stuff-stuff-stuff-stuff");

            var path = Path.Combine(_rootFileDirectory + "File", $"{_Id}.csv");
            var filePath = path.Contains("https://") ? path.Replace('\\', '/') : path;
            var listOfObjects = Parser(filePath); <-----
            return listOfObjects;
        }

        private IList<blabla> Parser(string filePath)
        {
            var Data = File.ReadLines(filePath); <-----
            foreach...
        }

The path that keeps failing is:

<add key="stuff-stuff-stuff-stuff" value="https://*******.blob.core.windows.net/******/" />

Again, when I run from my local (C:\Users\*******\Desktop\), it works fine. I'm guessing this is because File.Readlines() isn't able to read https paths. Can anyone confirm this and help me find a work-around with the same functionality?

Thanks in advance for helping a noob out.

Fissure
  • 229
  • 4
  • 9
  • 1
    Maybe this would answer your question: https://stackoverflow.com/questions/12240857/how-to-get-content-from-file-from-this-url – devcrp Jun 13 '20 at 05:24
  • 1
    Don't get distracted by the incorrect comment above, you will want to use the .NET SDK for Azure Storage. [See this for a walkthrough](https://medium.com/@rammonzito/azure-blob-storage-using-a-net-core-console-application-106a0c2e6de5) of using a `BlobClient` to download a blob. – Crowcoder Jun 13 '20 at 10:54
  • Is it useful for you? If it is helpful, could you please accept the solution as an answer? it may help more people. – Jim Xu Jun 16 '20 at 03:04

1 Answers1

1

If you want to read csv file stored in Azure blob, you need to download it from Azure blob storage at first then you can read its content. For more details, please refer to here and here

For example

  1. Install sdk
dotnet add package Azure.Storage.Blobs
  1. Code
            string accountName = "";
            string accountKey = "";
            string blobUri = "";
            StorageSharedKeyCredential keyCredential = new StorageSharedKeyCredential(accountName, accountKey);
            BlobClient blob = new BlobClient(new Uri(blobUri), keyCredential);
            BlobDownloadInfo downloadInfo = await blob.DownloadAsync();
            using (StreamReader reader = new StreamReader(downloadInfo.Content)) {
                var line = reader.ReadLine();
                var columnName = line.Split(',');
                while (!reader.EndOfStream)
                {

                    string splits = reader.ReadLine();
                    var rowValue = splits.Split(',');

                    // process the value as your need
                }

            }
Jim Xu
  • 21,610
  • 2
  • 19
  • 39