Using Azure fluent API-s it is easy to create storage accounts and blob containers. After experimenting with fluent API of Azure storage I found it to be good match for multitenant web applications where tenant files are held on Azure blob storage. I was surprised how clean and short code I got using fluent API. Here’s my overview of fluent API for Azure storage with some code samples I wrote.

Getting started

Before getting to code we need Azure portal account and some configuration parameters:

  • TenantId – ID of Azure AD tenant. Open Azure AD settings in Azure portal to get tenant ID.
  • ClientId and ClientSecret – these are for service principal (or account) we want to use for accessing Azure services. To set up service principal please jump to Azure documentation page How to: Use the portal to create an Azure AD application and service principal that can access resources.
  • SubscriptionId – ID of Azure subscription where we want to create storage accounts. Open Subscriptions in Azure portal to get subscription ID.
  • ResourceGroup – name of resource group under what storage accounts are created. Make sure the resource group exists already or create new one if you need.

With settings ready it’s time to get into code.

Fluent syntax for Azure blob storage

There are few important things to know before implementing class to create storage accounts. Fluent syntax for Azure services uses IAzure interface as a bridge head to row of fluent method calls. When building IAzure instance we need to provide all configuration parameters mentioned above.

Here’s the example. Azure.Configure() method starts the row of fluent configuring methods to build up client class we will use later.

var principalLogIn = new ServicePrincipalLoginInformation();
principalLogIn.ClientId = ClientId;
principalLogIn.ClientSecret = ClientSecret;

var environment = AzureEnvironment.AzureGlobalCloud;
var credentials = new AzureCredentials(principalLogIn, TenantId, environment);

_azure = Azure.Configure()

To create blob storage account we will use initialized Azure client like shown in the following example.

await _azure.StorageAccounts.Define(accountName)

Although fluent methods shown above are just few of those provided, notice how new account is defined using clear and readable fluent methods that specify important aspects of account. Finally CreateAsync() method is called and then new storage account is created.

Similar fluent API-s are available also for some other Azure services and in big part they follow the same idea as shown here with Azure storage account.

#azure #api-s #programming

Creating Storage Accounts and bBlob Containers Using Azure fluent API-s
1.25 GEEK