将Azure blob的内容作为文本字符串下载需要很长时间

时间:2015-08-13 08:51:05

标签: c# csv azure azure-storage-blobs memorystream

我正在开发一个

的应用程序
  1. 使用简单的HTTP网页(REST方法)从本地计算机上载Azure blob存储上的.CSV文件

  2. 一旦上传了.CSV文件,我就会获取流来更新我的数据库

  3. .CSV文件大约30 MB,上传到blob需要2分钟,但需要30分钟才能读取流您能否提供输入以提高速度? 以下是用于从文件中读取流的代码段: https://azure.microsoft.com/en-in/documentation/articles/storage-dotnet-how-to-use-blobs/

    public string GetReadData(string filename)
            {
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["StorageConnectionString"]);
    
                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
                // Retrieve reference to a previously created container.
                CloudBlobContainer container = blobClient.GetContainerReference(System.Web.Configuration.WebConfigurationManager.AppSettings["BlobStorageContainerName"]);
    
                // Retrieve reference to a blob named "filename"
                CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(filename);
    
                string text;
                using (var memoryStream = new MemoryStream())
                {
                    blockBlob2.DownloadToStream(memoryStream);
                    text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
                }
    
                return text;
            }
    

1 个答案:

答案 0 :(得分:3)

为了加快这个过程,你可以做的一件事就是不是一次性读取整个文件而是以块的形式阅读它们。看看DownloadRangeToStream方法。

基本上,您的想法是首先创建一个30 MB的空文件(blob的大小)。然后并行使用DownloadRangeToStream方法下载1MB(或任何你认为合适的大小)块。当下载这些块时,您将流内容放在文件中的适当位置。

我几天前回答了类似的问题:StorageException when downloading a large file over a slow network。看看我的答案。在那里按顺序下载块,但它应该让你知道如何实现chunked下载。

相关问题