一个Excel文件托pipe在Azure Blob上,如何将其读入FileStream?

我使用C#将file upload到Azure Blob。 现在我想用ExcelDataReader读取上传的文件。

我正在使用下面的代码。 其中_imageRootPathNos是保存文件的path( http://imor.blob.core.windows.net/files )

FileStream stream = System.IO.File.Open(_imageRootPathNos + "/" + "ImEx.xlsx", FileMode.Open, FileAccess.Read); 

我得到一个错误System.ArgumentException:'URI格式不支持。

我错过了什么?

ExcelDataReader可以从任何stream读取数据,而不仅仅是一个FileStream。 您可以使用WebClient(过时),HttpClient或Azure SDK打开stream并读取blob。

读取或下载一个blob打开并读取一个stream。 而不是例如。 下载blob或将其所有内容读取到缓冲区中,则直接访问该stream。 无论您使用哪种技术,最终都会通过单个URL打开一个stream以供阅读。

在你的情况下,你可以下载并保持文件重用,或者你可以直接从stream中读取。 如果您没有权限写入磁盘文件,或者如果同时处理多个请求并且不想处理临时文件存储,则可能需要在Web应用程序中执行此操作。

使用HttpClient,你可以使用GetStreamAsync方法打开一个stream:

 var client=new HttpClient(); client.BaseAddress = new Uri("https://imor.blob.core.windows.net/files"); // Set headers and credentials // ... using(var stream=await client.GetStreamAsync("ImEx.xlsx")) { var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //Process the data } 

使用Azure SDK,您可以使用CloudBlob.OpenRead方法:

 var blob = container.GetBlockBlobReference("Imex.xlsx"); using(var stream=await blob.OpenReadAsync()) { var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //Process the data } 

您可能希望将数据存储在内存缓冲区或文件中,例如用于caching或重新处理。 为此,您可以分别创build一个MemoryStream或FileStream,并将blobstream中的数据复制到目标stream。

使用HttpClient,你可以填充一个内存缓冲区:

 //To avoid reallocations, create a buffer large enough to hold the file using(var memStream=new MemoryStream(65536)) { using(var stream=await client.GetStreamAsync("ImEx.xlsx")) { await stream.CopyToAsync(memStream); } memStream.Position=0; var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memStream); } 

使用SDK:

 using(var memStream=new MemoryStream(65536)) { //..... var blob = container.GetBlockBlobReference("Imex.xlsx"); await stream.DownloadToStreamAsync(memStream); memStream.Position=0; var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memStream); //... } 

要下载文件,您可以使用FileStreamreplaceMemoryStream。

您不能使用标准FileSteam访问Azure Blob存储文件。 正如克里斯的回答中所build议的,您可以使用Azure SDK来访问文件。 或者,您可以使用Azure Blob服务API 。

另一个解决scheme是使用Azure文件存储并创build一个到文件存储的映射networking驱动器。 然后,您可以使用您的代码访问该文件,就好像它位于本地或networking存储系统上一样。

这两种服务之间有许多技术上的区别。 根据定价,Azure文件存储比Azure Blob存储更昂贵,但是根据预期用途,两者都相当便宜。

在使用Azure存储服务时,build议您使用Azure .NET SDK。 该SDK公开了适当的方法来下载,上传和pipe理你的容器和blob存储。 在这种情况下,你的代码应该是这样的:

 // Retrieve storage account from connection string. CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a previously created container. CloudBlobContainer container = blobClient.GetContainerReference("files"); // Retrieve reference to a blob named "imex.xlsx". CloudBlockBlob blockBlob = container.GetBlockBlobReference("Imex.xlsx"); // Save blob contents to a file. using (var fileStream = System.IO.File.OpenWrite(@"path\myfile")) { blockBlob.DownloadToStream(fileStream); } 

您可以在这里find所有关于如何使用SDK的信息: https : //docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-blobs

我使用这块代码来读取excel文件(上传到azure)到数据集中

 Uri blobUri = new Uri(_imageRootPath + "/" + fileName); var wc = new WebClient(); var sourceStream = wc.DownloadData(blobUri); Stream memoryStream = new MemoryStream(sourceStream); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(memoryStream); DataSet dsResult = excelReader.AsDataSet(); return dsResult;