C#大文件下载可从azure blob存储恢复

C#大文件下载可从azure blob存储恢复,c#,azure,azure-storage,C#,Azure,Azure Storage,我真的需要一些橡皮鸭 我有一个至少2.3 GiB的文件。 我目前正在将此文件下载到临时目录。 但是当下载被中断(连接错误或windows崩溃)时,我希望用户在停止的地方继续下载。不要再下载整个文件了。 代码的工作原理是继续下载文件,但我看到下载流又从头开始。这意味着文件的结尾是(2.3 GiB+先前下载的字节数),ofc会破坏我的文件 我使用了下面的代码片段来恢复下载,所以我希望流能够继续,在停止的地方 localStream.Seek(positionInFile,SeekOrigin.Beg

我真的需要一些橡皮鸭

我有一个至少2.3 GiB的文件。 我目前正在将此文件下载到临时目录。 但是当下载被中断(连接错误或windows崩溃)时,我希望用户在停止的地方继续下载。不要再下载整个文件了。 代码的工作原理是继续下载文件,但我看到下载流又从头开始。这意味着文件的结尾是(2.3 GiB+先前下载的字节数),ofc会破坏我的文件

我使用了下面的代码片段来恢复下载,所以我希望流能够继续,在停止的地方 localStream.Seek(positionInFile,SeekOrigin.Begin)

你知道我在这里遗漏了什么吗? 这是我的密码

                BlobContainerClient containerClient = new BlobContainerClient(connectionString, container);
                var blobClient = containerClient.GetBlobClient(downloadFile);

                fullOutputPath = createOutputFilePath(updateFileUri.OriginalString, outputFolder);
                downloadFileInfo = new FileInfo(fullOutputPath);

                var response = blobClient.Download(cts.Token);
                contentLength = response.Value.ContentLength;
                if (contentLength.HasValue && contentLength.Value > 0)
                {
                    if (_fileSystemService.FileExists(fullOutputPath))
                    {
                        from = downloadFileInfo.Length;
                        to = contentLength;

                        if (from == to)
                        {
                            //file is already downloaded
                            //skip it
                            progress.Report(1);
                            return;
                        }
                        fileMode = FileMode.Open;
                        positionInFile = downloadFileInfo.Length;
                    }

                    using FileStream localStream = _fileSystemService.CreateFile(fullOutputPath, fileMode, FileAccess.Write);
                    localStream.Seek(positionInFile, SeekOrigin.Begin);

                    bytesDownloaded = positionInFile;
                    double dprog = ((double)bytesDownloaded / (double)(contentLength.Value + positionInFile));
                    do
                    {
                        bytesRead = await response.Value.Content.ReadAsync(buffer, 0, buffer.Length, cts.Token);

                        await localStream.WriteAsync(buffer, 0, bytesRead, cts.Token);
                        await localStream.FlushAsync();

                        bytesDownloaded += bytesRead;
                        dprog = ((double)bytesDownloaded / (double)(contentLength.Value + positionInFile));
                        progress.Report(dprog);

                    } while (bytesRead > 0);
                }

我为您做了一些测试,在我的例子中,我使用一个.txt文件来演示您的需求。您可以看到.txt文件。 如您所见,在第151行,我做了一个结束标记:

我还创建了一个以该结束标记结尾的本地文件,以模拟下载被中断,我们将继续从存储下载:

下面是我的快速演示代码:

static void Main(string[] args)
{
    string containerName = "container name";
    string blobName = ".txt file name";
    string storageConnStr = "storage account conn str";

    string localFilePath = @"local file path";

    var localFileStream = new FileStream(localFilePath, FileMode.Append);

    var localFileLength = new FileInfo(localFilePath).Length;

    localFileStream.Seek(localFileLength, SeekOrigin.Begin);


    var blobServiceClient = new BlobServiceClient(storageConnStr);
    var blobClient = blobServiceClient.GetBlobContainerClient(containerName).GetBlobClient(blobName);

    var stream = blobClient.Download(new Azure.HttpRange(localFileLength)).Value.Content;

    var contentStrting = new StreamReader(stream).ReadToEnd();

    Console.WriteLine(contentStrting);

    localFileStream.Write(Encoding.ASCII.GetBytes(contentStrting));
    localFileStream.Flush();
}
结果: 我们只下载了结束标记后面的内容:

内容已下载到本地.txt文件:


如果您还有任何问题,请告诉我。

您可以使用range分块下载
公共下载(HttpRange range=default,BlobRequestConditions=null,bool rangeGetContentHash=false,CancellationToken CancellationToken=default)这真的很有希望,我将测试它。。。谢谢buddyHi@Henkie85,你的问题解决了吗?是的,非常好,谢谢