首页 文章

Azure存储未找到csv文件

提问于
浏览
1

我正在尝试从我的azure存储帐户中读取csv文件 . 将每行转换为对象并构建这些对象的列表 . 它一直在犯错,原因是它找不到文件(找不到Blob) . 该文件在那里,它是一个csv文件 .

File in azure storage

错误:

StorageException:指定的blob不存在 . AzureFileMethods.cs中的BatlGroup.Site.Services.AzureStorageService.AzureFileMethods.ReadCsvFileFromBlobAsync(CloudBlobContainer容器,字符串fileName)等待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;

        }

    }

我确保该文件是公开的 . 我可以下载存储在那里的任何文本文件,但不能下载任何csv文件 .

我也不确定采用什么格式,因为我需要遍历这些行 .

我看到了将整个文件放到临时驱动器并在那里使用它的示例,但这似乎没有效果,因为我可以将文件存储在wwroot文件夹而不是azure中 .

从azure存储中读取csv文件的最合适方法是什么 .

1 回答

  • 0

    关于如何遍历这些行,在获得内存流之后,可以使用 StreamReader 逐行读取它们 .

    示例代码如下:

    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();
            }
        }
    }
    

    我的csv文件:
    enter image description here

    测试结果:
    enter image description here

相关问题