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

RDX*_*RDX 5 c# csv memorystream azure azure-storage-blobs

我正在开发一个应用程序

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

  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;
        }
Run Code Online (Sandbox Code Playgroud)

Gau*_*tri 6

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

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

几天前我在 SO 上回答了一个类似的问题:通过慢速网络下载大文件时出现 StorageException。看看我在那里的回答。在那里,块按顺序下载,但它应该让您了解如何实现分块下载。