Access public containers and blobs anonymously with .NET

Azure Storage supports optional public read access for containers and blobs. Clients can access public containers and blobs anonymously by using the Azure Storage client libraries, as well as by using other tools and utilities that support data access to Azure Storage.

This article shows how to access a public container or blob from .NET. For information about configuring anonymous read access on a container, see Configure anonymous public read access for containers and blobs. For information about preventing all anonymous access to a storage account, see Prevent anonymous public read access to containers and blobs.

A client that accesses containers and blobs anonymously can use constructors that do not require credentials. The following examples show a few different ways to reference containers and blobs anonymously.

Create an anonymous client object

You can create a new service client object for anonymous access by providing the Blob storage endpoint for the account. However, you must also know the name of a container in that account that's available for anonymous access.

public static void CreateAnonymousBlobClient()
{
    // Create the client object using the Blob storage endpoint for your account.
    BlobServiceClient blobServiceClient = new BlobServiceClient
        (new Uri(@"https://storagesamples.blob.core.windows.net/"));

    // Get a reference to a container that's available for anonymous access.
    BlobContainerClient container = blobServiceClient.GetBlobContainerClient("sample-container");

    // Read the container's properties. 
    // Note this is only possible when the container supports full public read access.          
    Console.WriteLine(container.GetProperties().Value.LastModified);
    Console.WriteLine(container.GetProperties().Value.ETag);
}

Reference a container anonymously

If you have the URL to a container that is anonymously available, you can use it to reference the container directly.

public static void ListBlobsAnonymously()
{
    // Get a reference to a container that's available for anonymous access.
    BlobContainerClient container = new BlobContainerClient
        (new Uri(@"https://storagesamples.blob.core.windows.net/sample-container"));

    // List blobs in the container.
    // Note this is only possible when the container supports full public read access.
    foreach (BlobItem blobItem in container.GetBlobs())
    {
        Console.WriteLine(container.GetBlockBlobClient(blobItem.Name).Uri);
    }
}

Reference a blob anonymously

If you have the URL to a blob that is available for anonymous access, you can reference the blob directly using that URL:

public static void DownloadBlobAnonymously()
{
    BlockBlobClient blob = new BlockBlobClient
        (new Uri(@"https://storagesamples.blob.core.windows.net/sample-container/logfile.txt"));
    blob.DownloadTo(@"C:\Temp\logfile.txt");
}

Next steps