سؤال

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.

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);
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top