Azure download blob part

本文关键字:part blob download Azure | 更新日期: 2023-09-27 17:55:36

如果有人有DownloadRangeToStream功能的经验,我将不胜感激。

在这里他们说参数"长度"是数据的长度,但根据我的经验,它是要下载的段的上部位置,例如"长度"-"偏移量"=数据的实际长度。

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

感谢您的任何帮助

Azure download blob part

试试这段代码。它通过将大型 blob 拆分为 1 MB 块来下载该块。

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