services | platforms | author |
---|---|---|
azure-resource-manager, azure-storeage |
dotnet |
guanghu |
This sample explains how to manage your resources and storage services in Azure Stack using the Azure .NET SDK.
On this page
- This sample requires to be run either from the Azure Stack Development Kit(ASDK) or from an external client if you are connected through VPN.
- If you don't have it, install the .NET Core SDK.
- Recommand to Install and configure CLI for use with Azure Stack
- Clone the repository.
git clone https://github.com/guanghuthegreat/azurestack-storage-resources-sample-dotnet.git
- Install the dependencies.
dotnet restore
- Create an Azure service principal either through
Azure CLI,
PowerShell
or the portal.
In Azure Stack give Contributor permissions to the subscription where the resources are stored.
An quick way is to run the following Azure CLI cmdlet on Azure Stack and record the appId and password as client id and client secret key.
az ad sp create-for-rbac -n {choose a name for sp}
- Obtain the value for your account subscription ID and tenant ID. You can find these info by running CLI cmdlet
az account show
on the target Azure Stack and find out these value in the fields id and tenantId from the cmdlet outputs - Obtain the URI for AAD login, AAD resource ID and endpoints for management, storage. You can easily find out these info by running CLI cmdlet
az cloud show
on the target Azure Stack and find out these value in the fields of activeDirectory, activeDirectoryResourceId, management and storageEndpoint from this cmdlet outputs. - Export these environment variables and fill in the values you created in previous steps.
Set AZS_ACTTIVEDIRECTORY={the AAD login URI} Set AZS_ACTTIVEDIRECTORYRESOURCEID={the AAD resource ID} Set AZS_MANAGEMENTENDPOINT={the management endpoint URI} Set AZS_STORAGENDPOINT={the storage endpoint URI} Set AZS_SUBID={your subscription id} Set AZS_TENANTID={your tenant id} Set AZS_CLIENTID={your client id} Set AZS_SECRETKEY={your client secret key} Set AZS_LOCATION={the location (region) of your Azure Stack deployment, like 'local' in a ASDK deployments}
- Run the sample.
dotnet run
The sample walks you through several resrouce group and storage services operations. It starts by setting up a ResourceManagementClient and StorageManagementClient objects using your subscription and credentials.
In order to access to the specific Azure Stack you want to operate, you need to customize a ActiveDirectoryServiceSettings with the value of AzS_ActiveDirectory and AzS_ActiveDirectoryResourceID we prepared in previous steps pass it for login. Otherwise, your operation will just go to the public Azure as a default behavior.
ActiveDirectoryServiceSettings s = new ActiveDirectoryServiceSettings();
s.AuthenticationEndpoint = new Uri(AzS_ActiveDirectory);
s.TokenAudience = new Uri(AzS_ActiveDirectoryResourceID);
s.ValidateAuthority = true;
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(AzS_TenantID, AzS_ClientID, AzS_SecretKey, s);
The mangement endpoint for resource management client object in Azure Stack has to be replaced by the value of AzS_ManagementEndPoint we prepared in previous steps. This is for the specific Azure Stack we need to manage.
var resourceClient = new ResourceManagementClient(creds)
{
BaseUri = new Uri(AzS_ManagementEndPoint),
SubscriptionId = AzS_SubscriptionID
};
The mangement endpoint for storage management client object in Azure Stack has to be replaced by the value of AzS_ManagementEndPoint we prepared in previous steps. This is for the specific Azure Stack we need to manage.
var storageClient = new StorageManagementClient(creds)
{
BaseUri = new Uri(AzS_ManagementEndPoint),
SubscriptionId = AzS_SubscriptionID
};
In Azure Stack, there are some differences compared to public Azure, for example, Azure Stack storage supports LRS and gerneral-purpose storage account type, so we need to identify these value correctly when creating a storage account. For more information in Azure Stack Storage: Differences and considerations
// set default parameters for storage account in Azure Stack
public static Microsoft.Azure.Management.Storage.Models.Sku DefaultSku = new Microsoft.Azure.Management.Storage.Models.Sku(SkuName.StandardLRS); // Azure Stack only supports LRS
public static Kind DefaultStorageKind = Kind.Storage; // Azure Stack only supports general-purpose stroage account type
public static Dictionary<string, string> DefaultTags = new Dictionary<string, string>
{
{"key1","value1"},
{"key2","value2"}
};
// create storage accounts
StorageAccountCreateParameters parameters = new StorageAccountCreateParameters
{
Location = AzS_Location,
Kind = DefaultStorageKind,
Tags = DefaultTags,
Sku = DefaultSku
};
var storageAccount = storageMgmtClient.StorageAccounts.Create(rgname, acctName, parameters);
When creating a blob container in Azure Stack Storage service, only one thing needs to be specified, customized the storage endpoint uri during the CloudStorageAccount initializaiton. How to prepare the Az_StorageEndPoint you can find details in previous steps.
StorageCredentials cre = new StorageCredentials(accountName, key);
CloudStorageAccount storageAccount = new CloudStorageAccount(cre, AzS_StorageEndPoint, true); // specify the value of storage endpoint for Azure Stack
CloudBlobClient blob = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blob.GetContainerReference(blobcontainerName);
blobContainer.CreateIfNotExists();