加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 通过慢速网络下载大文件时的StorageException

发布时间:2020-12-15 23:26:24 所属栏目:百科 来源:网络整理
导读:我正在使用NuGet包 WindowsAzure.Storage版本4.2.1. 以下代码尝试从远程数据中心的存储容器中下载blob. try{ var blobRequestOptions = new BlobRequestOptions { RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5),3),MaximumExecutionTime = Ti
我正在使用NuGet包 WindowsAzure.Storage版本4.2.1.

以下代码尝试从远程数据中心的存储容器中下载blob.

try
{
    var blobRequestOptions = new BlobRequestOptions
    {
        RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5),3),MaximumExecutionTime = TimeSpan.FromMinutes(60),ServerTimeout = TimeSpan.FromMinutes(60)
    };
    using (var fileStream = File.Create(localPath))
    {
        blockBlob.DownloadToStream(fileStream,null,blobRequestOptions);
    }
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

但是,有时它下载约10分钟,然后抛出以下异常:

Unhandled Exception: Microsoft.WindowsAzure.Storage.StorageException: The client could not finish the operation within specified timeout. ---> System.TimeoutException: The client could not finish the operation within specified timeout.
--- End of inner exception stack trace ---
at Microsoft.WindowsAzure.Storage.Core.Util.StorageAsyncResult`1.End()
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.EndUploadText(IAsyncResult asyncResult)
at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__DisplayClass4.b__3(IAsyncResult ar)
--- End of stack trace from previous location where exception was thrown ---

我怎样才能解决这个问题?

解决方法

请试试这段代码.基本上它的作用是首先创建一个空文件然后使用 DownloadRangeToStream方法以1 MB块的形式读取blob的数据.随着块的下载,它会将其附加到文件中.

private static void DownloadLargeFile()
    {
        var account = new CloudStorageAccount(new StorageCredentials(accountName,accountKey),true);
        var blobClient = account.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("container-name");
        var file = "my-very-large-file-name";
        var blob = container.GetBlockBlobReference(file);
        //First fetch the size of the blob. We use this to create an empty file with size = blob's size
        blob.FetchAttributes();
        var blobSize = blob.Properties.Length;
        long blockSize = (1 * 1024 * 1024);//1 MB chunk;
        blockSize = Math.Min(blobSize,blockSize);
        //Create an empty file of blob size
        using (FileStream fs = new FileStream(file,FileMode.Create))//Create empty file.
        {
            fs.SetLength(blobSize);//Set its size
        }
        var blobRequestOptions = new BlobRequestOptions
        {
            RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5),ServerTimeout = TimeSpan.FromMinutes(60)
        };
        long currentPointer = 0;
        long bytesRemaining = blobSize;
        do
        {
            var bytesToFetch = Math.Min(blockSize,bytesRemaining);
            using (MemoryStream ms = new MemoryStream())
            {
                //Download range (by default 1 MB)
                blob.DownloadRangeToStream(ms,currentPointer,bytesToFetch,blobRequestOptions);
                ms.Position = 0;
                var contents = ms.ToArray();
                using (var fs = new FileStream(file,FileMode.Open))//Open that file
                {
                    fs.Position = currentPointer;//Move the cursor to the end of file.
                    fs.Write(contents,contents.Length);//Write the contents to the end of file.
                }
                currentPointer += contents.Length;//Update pointer
                bytesRemaining -= contents.Length;//Update bytes to fetch
            }
        }
        while (bytesRemaining > 0);
    }

我在一个小文件上尝试了这个代码(糟糕的互联网连接:P).因此,在尝试使用500 MB文件之前,请先在一个小文件(比如大约5 – 10 MB)上尝试一下. HTH.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读