简体   繁体   English

Azure下载blob部分

[英]Azure download blob part

I would be very grateful if anybody has experience with the function DownloadRangeToStream. 如果有人有使用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, eg "length" - "offset" = real length of the data. 这里他们说参数“length”是数据的长度,但根据我的经验,它是要下载的段的上部位置,例如“length” - “offset”=数据的实际长度。

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,因为前面提到的函数似乎不起作用。

Thank you for any help 感谢您的任何帮助

Try this code. 试试这个代码。 It downloads a large blob by splitting it in 1 MB chunks. 它通过将其拆分为1 MB块来下载大blob。

    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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM