微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取 CloudBlob.DownloadToFileParallelAsync 的进度

如何解决获取 CloudBlob.DownloadToFileParallelAsync 的进度

我正在尝试从我的 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);

解决方法

通过一种变通方法解决了它。我没有使用 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,progressHandler,cancellationToken);
                    }
                    else
                    {
                        await cloudModuleBlob.DownloadRangeToStreamAsync(fileStream,chunkSize,cancellationToken);
                    }


                    current = current + chunkSize;

                }
                
               
            }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。