将 Azure blob 的内容下载为文本字符串花费的时间太长

Downloading contents of a Azure blob as a text string taking too long time

我正在开发一个应用

  1. 使用简单的 HTTP 网页(REST 方法)从我的本地计算机将 .CSV 文件上传到 Azure blob 存储

  2. .CSV 文件上传后,我获取流以更新我的数据库

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

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

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

前几天我在SO上回答了一个类似的问题:。看看我的答案。那里的块是按顺序下载的,但它应该让您了解如何实现分块下载。