Authenticating BlobContainerClient with Azure SDK for Java

Elon Azoulay 0 Reputation points
2025-06-27T21:52:09.7366667+00:00

What are the recommended methods for authenticating com.azure.storage.blob.BlobContainerClient to allow user access to a blob container and its blobs? There appear to be multiple approaches to manage permissions and credentials. Can you provide some examples for creating a BlobContainerClient with the necessary access? Much appreciated! Also going through the docs and will post a few answers if I find them.

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,548 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Divyesh Govaerdhanan 6,560 Reputation points
    2025-06-27T23:03:24.65+00:00

    Hello,

    Welcome to Microsoft Q&A,

    1. Using Azure Identity (Recommended for Managed Identity & Azure AD):

    Use DefaultAzureCredential to automatically select the best credential (ideal for Managed Identity, Environment variables, or developer login).

    import com.azure.identity.DefaultAzureCredentialBuilder;
    import com.azure.storage.blob.BlobContainerClient;
    import com.azure.storage.blob.BlobContainerClientBuilder;
    
    BlobContainerClient client = new BlobContainerClientBuilder()
        .credential(new DefaultAzureCredentialBuilder().build())
        .endpoint("https://<your-storage-account>.blob.core.windows.net/<your-container>")
        .buildClient();
    
    1. Using a Connection String (Not recommended for production)
    String connectionString = "DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net";
    BlobContainerClient client = new BlobContainerClientBuilder()
        .connectionString(connectionString)
        .containerName("your-container-name")
        .buildClient();
    
    1. Using a Shared Access Signature (SAS)

    Use a SAS token to give limited access.

    String sasUrl = "https://<your-storage-account>.blob.core.windows.net/<your-container>?<sas-token>";
    
    BlobContainerClient client = new BlobContainerClientBuilder()
        .endpoint(sasUrl)
        .buildClient();
    
    

    https://learn.microsoft.com/en-us/java/api/overview/azure/storage-blob-readme?view=azure-java-stable#authenticate-the-client

    https://learn.microsoft.com/en-us/java/api/overview/azure/identity-readme?view=azure-java-stable

    Please Upvote and accept the answer if it helps!!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.