Azure download blob part

后端 未结 1 1817
日久生厌
日久生厌 2021-01-03 02:47

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, bu

相关标签:
1条回答
  • 2021-01-03 03:13

    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);
        }
    
    0 讨论(0)
提交回复
热议问题