Azure下载blob部分

问题描述 投票:4回答:1

如果有人有使用DownloadRangeToStream功能的经验,我将非常感激。

Here他们说参数“长度”是数据的长度,但根据我的经验,它是要下载的段的上部位置,例如“length” - “offset”=数据的实际长度。

我真的很感激,如果有人能给我一些代码来下载块中的blob,因为前面提到的函数似乎不起作用。

感谢您的任何帮助

c# azure azure-storage
1个回答
8
投票

试试这个代码。它通过将其拆分为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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.