Developing Solutions for Azure (AZ-204)

Last Updated: 7/9/2024

Creating Client Object

  • You create client objects to interact with the three types of resources in the storage service: storage accounts, containers, and blobs.
  • You pass a URI referencing the endpoint to the client constructor.
  • You can use DefaultAzureCredential to authenticate to Azure via a Microsoft Entra security principal. The authentication process includes obtaining an access token for authorization. This access token is passed as a credential when the client is instantiated, and the credential persists throughout the client lifetime.
  • The Microsoft Entra security principal requesting the token must be assigned an appropriate Azure RBAC role that grants access to blob data.

BlobServiceClient

public BlobServiceClient GetBlobServiceClient(string accountName)
{
    BlobServiceClient client = new(
        new Uri($"https://{accountName}.blob.core.windows.net"),
        new DefaultAzureCredential());

    return client;
}

BlobContainerClient

public BlobContainerClient GetBlobContainerClient(
    BlobServiceClient blobServiceClient,
    string containerName)
{
    // Create the container client using the service client object
    BlobContainerClient client = blobServiceClient.GetBlobContainerClient(containerName);
    return client;
}

BlobClient

public BlobClient GetBlobClient(
    BlobServiceClient blobServiceClient,
    string containerName,
    string blobName)
{
    BlobClient client =
        blobServiceClient.GetBlobContainerClient(containerName).GetBlobClient(blobName);
    return client;
}