Skip to content

Latest commit

 

History

History
174 lines (125 loc) · 7.59 KB

File metadata and controls

174 lines (125 loc) · 7.59 KB

Azure Storage Management client library for .NET

This package follows the new Azure SDK guidelines which provide a number of core capabilities that are shared amongst all Azure SDKs, including the intuitive Azure Identity library, an HTTP Pipeline with custom policies, error-handling, distributed tracing, and much more.

Getting started

Install the package

Install the Azure Storage management library for .NET with NuGet:

Install-Package Azure.ResourceManager.Storage -Version 1.0.0-beta1

Prerequisites

Set up a way to authenticate to Azure with Azure Identity.

Some options are:

More information and different authentication approaches using Azure Identity can be found in this document.

Authenticate the Client

The default option to create an authenticated client is to use DefaultAzureCredential. Since all management APIs go through the same endpoint, in order to interact with resources, only one top-level ArmClient has to be created.

To authenticate to Azure and create an ArmClient, do the following:

using Azure.Identity;
using Azure.ResourceManager;

ArmClient armClient = new ArmClient(new DefaultAzureCredential());

Additional documentation for the Azure.Identity.DefaultAzureCredential class can be found in this document.

Key concepts

Key concepts of the Azure .NET SDK can be found here

Examples

Create a storage account

Before creating a storage account, we need to have a resource group.

ArmClient armClient = new ArmClient(new DefaultAzureCredential());
Subscription subscription = armClient.DefaultSubscription;
string rgName = "myRgName";
Location location = Location.WestUS2;
ResourceGroupCreateOrUpdateOperation operation= await subscription.GetResourceGroups().CreateOrUpdateAsync(rgName, new ResourceGroupData(location));
ResourceGroup resourceGroup = operation.Value;

Then we can create a storage account inside this resource group.

//first we need to define the StorageAccountCreateParameters
Sku sku = new Sku(SkuName.StandardGRS);
Kind kind = Kind.Storage;
string location = "westus2";
StorageAccountCreateParameters parameters = new StorageAccountCreateParameters(sku, kind, location);
//now we can create a storage account with defined account name and parameters
StorageAccountContainer accountContainer = resourceGroup.GetStorageAccounts();
string accountName = "myAccount";
StorageAccountCreateOperation accountCreateOperation = await accountContainer.CreateOrUpdateAsync(accountName, parameters);
StorageAccount storageAccount = accountCreateOperation.Value;

Get all storage accounts in a resource group

StorageAccountContainer accountContainer = resourceGroup.GetStorageAccounts();
AsyncPageable<StorageAccount> response = accountContainer.GetAllAsync();
await foreach (StorageAccount storageAccount in response)
{
    Console.WriteLine(storageAccount.Id.Name);
}

Get a storage account

StorageAccountContainer accountContainer = resourceGroup.GetStorageAccounts();
StorageAccount storageAccount = await accountContainer.GetAsync("myAccount");
Console.WriteLine(storageAccount.Id.Name);

Try to get a storage account if it exists

StorageAccountContainer accountContainer = resourceGroup.GetStorageAccounts();
StorageAccount storageAccount = await accountContainer.GetIfExistsAsync("foo");
if (storageAccount != null)
{
    Console.WriteLine(storageAccount.Id.Name);
}
if (await accountContainer.CheckIfExistsAsync("bar"))
{
    Console.WriteLine("storage account 'bar' exists");
}

Delete a storage account

StorageAccountContainer accountContainer = resourceGroup.GetStorageAccounts();
StorageAccount storageAccount = await accountContainer.GetAsync("myAccount");
await storageAccount.DeleteAsync();

Add a tag to the storage account

StorageAccountContainer accountContainer = resourceGroup.GetStorageAccounts();
StorageAccount storageAccount = await accountContainer.GetAsync("myAccount");
// add a tag on this storage account
await storageAccount.AddTagAsync("key", "value");

For more detailed examples, take a look at samples we have available.

Troubleshooting

Next steps

More sample code

Additional Documentation

For more information on Azure SDK, please refer to this website.

Contributing

For details on contributing to this repository, see the contributing guide.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions