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.
Before getting to code we need Azure portal account and some configuration parameters:
With settings ready it’s time to get into code.
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()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription(SubscriptionId);
To create blob storage account we will use initialized Azure client like shown in the following example.
await _azure.StorageAccounts.Define(accountName)
.WithRegion(Region.EuropeWest)
.WithExistingResourceGroup(ResourceGroup)
.WithAccessFromAllNetworks()
.WithGeneralPurposeAccountKindV2()
.WithOnlyHttpsTraffic()
.WithSku(StorageAccountSkuType.Standard_LRS)
.CreateAsync();
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