简体   繁体   中英

Azure download blob part

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, eg "length" - "offset" = real length of the data.

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.

Thank you for any help

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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