【问题标题】:Downloading contents of a Azure blob as a text string taking too long time将 Azure blob 的内容作为文本字符串下载需要很长时间
【发布时间】:2015-08-14 02:29:00
【问题描述】:

我正在开发一个应用程序

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

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

.CSV 文件大约 30 MB,上传到 blob 需要 2 分钟,但读取流需要 30 分钟您能否提供意见以提高速度? 这是用于从文件中读取流的代码 sn-p: 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;
        }

【问题讨论】:

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


    【解决方案1】:

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

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

    几天前我在 SO 上回答了一个类似的问题:StorageException when downloading a large file over a slow network。看看我那里的答案。这些块是按顺序下载的,但它应该让您了解如何实现分块下载。

    【讨论】:

    • Gaurav 已经完美地回答了这个问题,但我个人仍然建议您不要将应用程序放在本地计算机中。 :) 老实说,在单线程中花 30 分钟下载 30MB 文件是非常糟糕的。请考虑将您的应用程序移至 Azure(Web 角色/工作者角色/虚拟机)或网络环境更好的地方。
    • @GauravMantri 你能提供一个如何并行下载的例子吗?我看到您可以在BlobRequestOptions 中设置ParallelOperationThreadCount,但这仅适用于上传。找不到有关该主题的任何代码。谢谢!
    • @GFoley ... 你看过我在此处发布的示例代码了吗:stackoverflow.com/questions/31128977/…
    • 我做到了。并对其进行了投票。问题是(如上所述)它仅演示如何按顺序上传块,而不是并行。
    • 啊....我明白了(感谢您的支持:))。您能否发布一个新问题,我会尝试为您破解一些代码并提供答案?
    猜你喜欢
    • 2011-11-12
    • 1970-01-01
    • 2016-04-03
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多