Open PDF file from Azure Blob storage

13 Dec 20231 minute to read

To load a PDF file from Azure blob storage, you can follow the steps below

Step 1: Create a simple console application

Project configuration window

Step 3: Install the Microsoft.Azure.Storage.Blob NuGet package as a reference to your project from the NuGet.org.

NuGet package installation

Step 4: Include the following namespaces in the Program.cs file.

using Microsoft.Azure.Storage;
    using Microsoft.Azure.Storage.Blob;

Step 5: Add the below code example to load a PDF from Azure blob storage.

// Parse the connection string to your Azure Storage Account.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

    // Create a client to interact with Blob storage.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Get a reference to the container name.
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

    // Get a reference to the block blob name.
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
    
    // Open a file stream to save the downloaded blob content.
    using (var fileStream = File.OpenWrite("sample.pdf"))
    {
        // Download the blob's content to the file stream.
        blockBlob.DownloadToStream(fileStream);
    }

You can download a complete working sample from GitHub.