After enabling versioning in azure storage account. How can be download the versions of a blob using versionID in .net?

Rajat Saxena (NON EA SC ALT) 41 Reputation points
2023-05-15T14:38:08.6633333+00:00

After enabling versioning in azure storage account. How can be download the versions of a blob using versionID in .net?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,600 questions
{count} votes

Accepted answer
  1. deherman-MSFT 35,316 Reputation points Microsoft Employee
    2023-05-15T18:28:28.28+00:00

    @Rajat Saxena (NON EA SC ALT)

    We have an example of list blob versions in .NET which you can modify to download the blob versions. Here is a sample that uses the DownloadTo method:

    private static void DownloadBlobVersions(BlobContainerClient blobContainerClient, string blobName)
    {
        try
        {
            // Call the listing operation, specifying that blob versions are returned.
            // Use the blob name as the prefix.
            var blobVersions = blobContainerClient.GetBlobs
                (BlobTraits.None, BlobStates.Version, prefix: blobName)
                .OrderByDescending(version => version.VersionId)
                .Where(blob => blob.Name == blobName);
    
            // Construct the URI for each blob version and download it to a local file.
            foreach (var version in blobVersions)
            {
                BlobUriBuilder blobUriBuilder = new BlobUriBuilder(blobContainerClient.Uri)
                {
                    BlobName = version.Name,
                    VersionId = version.VersionId
                };
    
                // Get a reference to the blob version using the URI.
                BlobClient blobClient = new BlobClient(blobUriBuilder.ToUri());
    
                // Download the blob version to a local file with the version ID as the suffix.
                string filePath = @"C:\Users\user\Downloads\" + blobName + "_" + version.VersionId;
                blobClient.DownloadTo(filePath);
    
                if ((bool)version.IsLatestVersion.GetValueOrDefault())
                {
                    Console.WriteLine("Current version downloaded: {0}", filePath);
                }
                else
                {
                    Console.WriteLine("Previous version downloaded: {0}", filePath);
                }
            }
        }
        catch (RequestFailedException e)
        {
            Console.WriteLine(e.Message);
            Console.ReadLine();
            throw;
        }
    }
    
    

    Hope this helps! Let me know if you have further questions.


    If you still have questions, please let us know in the "comments" and we would be happy to help you. Comment is the fastest way of notifying the experts.

    If the answer has been helpful, we appreciate hearing from you and would love to help others who may have the same question. Accepting answers helps increase visibility of this question for other members of the Microsoft Q&A community.

    Thank you for helping to improve Microsoft Q&A! User's image

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rajat Saxena (NON EA SC ALT) 41 Reputation points
    2023-09-19T08:53:14.18+00:00

    @Sumarigo-MSFT I would like to clarify the behavior of blob versioning in a scenario where it was initially disabled, then you updated the blob data, and finally, you enabled versioning for the first time. In this situation, the current version appears to be blank. Is this behavior expected?

    0 comments No comments