Tutorial: Download Azure Blob files in zip format using .NET Core API (Part 3) – Azure Blob Storage

Posted by

Download Azure Blob file in zip Format using .Net Core | Part-3

Download Azure Blob file in zip Format using .Net Core | Part-3

In this tutorial, we will continue our series on working with Azure Blob Storage and .Net Core API. In this part, we will learn how to download a file from Azure Blob Storage in zip format using .Net Core.

Step 1: Install Azure.Storage.Blobs Package

First, we need to install the Azure.Storage.Blobs package in our .Net Core API project. You can do this by running the following command in the package manager console:

Install-Package Azure.Storage.Blobs

Step 2: Create a Method to Download File in Zip Format

Next, create a method in your .Net Core API controller that will download the file from Azure Blob Storage in zip format. Here’s an example of how you can do this:

public async Task DownloadFileInZipFormat(string blobName)
{
    var connectionString = "your_blob_connection_string_here";
    var containerName = "your_container_name_here";

    var blobServiceClient = new BlobServiceClient(connectionString);
    var containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    
    using (var memoryStream = new MemoryStream())
    {
        await containerClient.GetBlobClient(blobName).DownloadToAsync(memoryStream);
        memoryStream.Position = 0;
        return File(memoryStream, "application/zip", blobName);
    }
}

Step 3: Invoke the Method from your API Endpoint

Finally, you can invoke the method we created in the previous step from your API endpoint. Here’s an example of how you can do this:

[HttpGet]
public async Task DownloadFileInZip()
{
    var blobName = "your_blob_name_here.zip";
    return await DownloadFileInZipFormat(blobName);
}

Conclusion

Now you have learned how to download a file from Azure Blob Storage in zip format using .Net Core. This can be useful when you need to download multiple files at once or want to reduce the size of the file being downloaded. Stay tuned for more tutorials on Azure Blob Storage and .Net Core!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@bibhu794
8 months ago

Nice presentation