Azure下载Blob部分 [英] Azure download blob part

查看:92
本文介绍了Azure下载Blob部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人对DownloadRangeToStream函数有经验,我将不胜感激.

I would be very grateful if anybody has experience with the function DownloadRangeToStream.

Here they say that the parameter "length" is the length of the data, but in my experience it is the upper position of the segment to download, e.g. "length" - "offset" = real length of the data.

如果有人能给我一些用于下载块的blob的代码,我也将不胜感激,因为前面提到的功能似乎无法正常工作.

I would also really appreciate if anybody could give me some code for downloading a blob in chunks, since the function mentioned before doesn't seem to work.

谢谢您的帮助

推荐答案

尝试此代码.它将一个大的blob分成1 MB的块下载.

Try this code. It downloads a large blob by splitting it in 1 MB chunks.

    static void DownloadRangeExample()
    {
        var cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        var containerName = "container";
        var blobName = "myfile.zip";
        int segmentSize = 1 * 1024 * 1024;//1 MB chunk
        var blobContainer = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(containerName);
        var blob = blobContainer.GetBlockBlobReference(blobName);
        blob.FetchAttributes();
        var blobLengthRemaining = blob.Properties.Length;
        long startPosition = 0;
        string saveFileName = @"D:\myfile.zip";
        do
        {
            long blockSize = Math.Min(segmentSize, blobLengthRemaining);
            byte[] blobContents = new byte[blockSize];
            using (MemoryStream ms = new MemoryStream())
            {
                blob.DownloadRangeToStream(ms, startPosition, blockSize);
                ms.Position = 0;
                ms.Read(blobContents, 0, blobContents.Length);
                using (FileStream fs = new FileStream(saveFileName, FileMode.OpenOrCreate))
                {
                    fs.Position = startPosition;
                    fs.Write(blobContents, 0, blobContents.Length);
                }
            }
            startPosition += blockSize;
            blobLengthRemaining -= blockSize;
        }
        while (blobLengthRemaining > 0);
    }

这篇关于Azure下载Blob部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆