1

我正在尝试从我的 Azure 存储帐户下载二进制文件。最初,我使用的是 CloudBlob.DownloadToFileAsync(),它允许我提供 IProgress 参数并获取传输的进度更新。

但是,在大于 2gb 的文件上,DownloadToFileAsync 挂起。根据文档,我需要使用 DownloadToFileParallelAsync 来下载更大的文件。我已经实现了这一点,并确认它现在可以工作,但现在我无法获得下载进度,因为它不提供 IProgress 参数。

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.blob.cloudblob.downloadtofileparallelasync?view=azure-dotnet-legacy

谁能指出我如何收集任何有用的进度数据,或提供解决方法?

            int parallelIOCount = SystemInfo.processorCount;
            long rangeSizeInBytes = 16 * Constants.MB;
            await cloudModuleBlob.DownloadToFileParallelAsync(targetTempModuleFile, FileMode.Create, parallelIOCount, rangeSizeInBytes, cancellationTokenSource.Token);

            progressSlider.value = 1.0f;


            //When the download is finished...
            //Rename the temp file to the full version.
            if (File.Exists(targetCiqModuleFile))
            {
                File.Delete(targetCiqModuleFile);
            }

            File.Move(targetTempModuleFile, targetCiqModuleFile);



            Debug.Log("Download saved to: " + targetCiqModuleFile);
4

1 回答 1

0

用解决方法解决了它。我没有使用 DownloadToFileAsync,而是使用 DownloadRangeToStreamAsync 将 blob 分成更小的部分,然后在客户端将它们组合起来。有效地使用 16mb 块。

            //Create the file.
            using (FileStream fileStream = File.Create(targetTempModuleFile))
            {

                long chunkSize = 16 * Constants.MB;

                Int64 current = 0;

                while (current < cloudModuleBlob.Properties.Length)
                {


                    if ((current + chunkSize) > cloudModuleBlob.Properties.Length)
                    {
                        await cloudModuleBlob.DownloadRangeToStreamAsync(fileStream, current, (cloudModuleBlob.Properties.Length - current), default, default, default, progressHandler, cancellationToken);
                    }
                    else
                    {
                        await cloudModuleBlob.DownloadRangeToStreamAsync(fileStream, current, chunkSize, default, default, default, progressHandler, cancellationToken);
                    }


                    current = current + chunkSize;

                }
                
               
            }
于 2020-12-24T01:38:28.363 回答