gpt4 book ai didi

c# - Azure Blob 存储 DownloadToStreamAsync 在网络更改期间挂起

转载 作者:行者123 更新时间:2023-11-30 15:50:56 24 4
gpt4 key购买 nike

我在 Microsoft.WindowsAzure.Storage v9.3.3 方面遇到了问题和 Microsoft.Azure.Storage.Blob v11.1.0 NuGet 库。特别是下载大文件时。如果您在“DownloadToStreamAsync”方法期间更改网络,则调用将挂起。我发现我的代码(处理大量文件)偶尔会挂起,我一直在努力缩小范围。我认为网络更改可能是触发 Azure Blob 存储库中某些故障的可靠方法。

有关该问题的更多信息;

  • 当我拔掉网络电缆时,我的计算机切换到 WiFi,但请求从未恢复
  • 如果我通过 WiFi 开始下载,然后插入网线,则会出现相同的错误
  • “ServerTimeout”属性永远不会使请求失败,或者按照 Documentation 的预期运行。
  • “MaximumExecutionTime”属性确实会使请求失败,但我们不想将自己限制在某个时间段内,特别是因为我们正在处理大文件

如果在调用过程中网络发生更改,以下代码 100% 都会失败。

static void Main(string[] args)
{
try
{
CloudStorageAccount.TryParse("<Connection String>", out var storageAccount);
var cloudBlobClient = storageAccount.CreateCloudBlobClient();
var container = cloudBlobClient.GetContainerReference("<Container Reference>");
var blobRef = container.GetBlockBlobReference("Large Text.txt");
Stream memoryStream = new MemoryStream();
BlobRequestOptions optionsWithRetryPolicy = new BlobRequestOptions() { ServerTimeout = TimeSpan.FromSeconds(5), RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(20), 4) };
blobRef.DownloadToStreamAsync(memoryStream, null, optionsWithRetryPolicy, null).GetAwaiter().GetResult();
Console.WriteLine("Completed");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
finally
{
Console.WriteLine("Finished");
}
}

我找到了this active issue在 Azure 存储 GitHub 中,但它似乎不活动。

我可以采取任何其他方法来可靠且高效地下载 blob 或使用此包时丢失的内容吗?

最佳答案

感谢 Mohit 的建议。

  • 创建一个任务以在后台检查流长度
  • 如果流在设定的时间段内没有增加,请取消 DownloadToStreamAsync

免责声明:我还没有围绕此代码编写测试,也没有编写如何使其以高性能方式运行,因为您不能对处理的每个文件都进行这样的等待。如果下载完成,我可能需要取消初始任务,我还不知道,我只是想让它先工作。我认为它还没有准备好投入生产。

// Create download cancellation token
var downloadCancellationTokenSource = new CancellationTokenSource();
var downloadCancellationToken = downloadCancellationTokenSource.Token;

var completedChecking = false;

// A background task to confirm the download is still progressing
Task.Run(() =>
{
// Allow the download to start
Task.Delay(TimeSpan.FromSeconds(2)).GetAwaiter().GetResult();

long currentStreamLength = 0;
var currentRetryCount = 0;
var availableRetryCount = 5;

// Keep the checking going during the duration of the Download
while (!completedChecking)
{
Console.WriteLine("Checking");
if (currentRetryCount == availableRetryCount)
{
Console.WriteLine($"RETRY WAS {availableRetryCount} - FAILING TASK");
downloadCancellationTokenSource.Cancel();
completedChecking = true;
}

if (currentStreamLength == memoryStream.Length)
{
currentRetryCount++;
Console.WriteLine($"Length has not increased. Incremented Count: {currentRetryCount}");
Task.Delay(TimeSpan.FromSeconds(10)).GetAwaiter().GetResult();
}
else
{
currentStreamLength = memoryStream.Length;
Console.WriteLine($"Download in progress: {currentStreamLength}");
currentRetryCount = 0;
Task.Delay(TimeSpan.FromSeconds(1)).GetAwaiter().GetResult();
}
}
});

Console.WriteLine("Starting Download");

blobRef.DownloadToStreamAsync(memoryStream, downloadCancellationToken).GetAwaiter().GetResult();

Console.WriteLine("Completed Download");
completedChecking = true;

Console.WriteLine("Completed");

关于c# - Azure Blob 存储 DownloadToStreamAsync 在网络更改期间挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com