【问题标题】:Substitute File.Readlines() with something that is able to read URL/https用能够读取 URL/https 的东西替换 File.Readlines()
【发布时间】:2020-10-02 22:55:30
【问题描述】:

我正在尝试解析位于 Azure blob 存储中的 .csv 文件。当我使用 File.Readlines() 从本地解析 .csv 文件时,它工作正常。当我传递 blob 的路径并进行调试时,我在File.Readlines() 收到以下消息:

这是我的代码:

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...
        }

不断失败的路径是:

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

再次,当我从本地 (C:\Users\*******\Desktop\) 运行时,它运行良好。我猜这是因为 File.Readlines() 无法读取 https 路径。谁能确认这一点并帮助我找到具有相同功能的解决方法?

提前感谢您帮助菜鸟。

【问题讨论】:

  • 也许这会回答你的问题:stackoverflow.com/questions/12240857/…
  • 不要被上面的错误评论分心,你会想要使用 .NET SDK for Azure Storage。 See this for a walkthrough 使用 BlobClient 下载 blob。
  • 对你有用吗?如果有帮助,您能否接受该解决方案作为答案?它可能会帮助更多的人。

标签: c# azure-blob-storage


【解决方案1】:

如果要读取存储在 Azure blob 中的 csv 文件,首先需要从 Azure blob 存储中下载它,然后才能读取其内容。更多详情请参考herehere

例如

  1. 安装 SDK
dotnet add package Azure.Storage.Blobs
  1. 代码
            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
                }

            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2013-10-13
    相关资源
    最近更新 更多