Skip to content

An example illustrating how to use .NET to manipulate Azure Stack resources and storage services.

License

Notifications You must be signed in to change notification settings

isabella232/azurestack-resources-storage-blob-dotnet-samples

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

services platforms author
azure-resource-manager, azure-storeage
dotnet
guanghu

Manage Azure Stack resource and storage with .Net

This sample explains how to manage your resources and storage services in Azure Stack using the Azure .NET SDK.

On this page

Run this sample

Prerequisits

  1. 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.
  2. If you don't have it, install the .NET Core SDK.
  3. Recommand to Install and configure CLI for use with Azure Stack

Steps

  1. Clone the repository.
    git clone https://github.com/guanghuthegreat/azurestack-storage-resources-sample-dotnet.git
    
  2. Install the dependencies.
    dotnet restore
    
  3. 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}
    
  4. 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
  5. 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.
  6. 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}
    
  7. Run the sample.
    dotnet run
    

What is program.cs doing?

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.

Login for Azure Stack:

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);

set up the ResourceManagementClient object

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
};

set up the StorageManagementClient object

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
};

create a storage account

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);

create a blob container

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();

About

An example illustrating how to use .NET to manipulate Azure Stack resources and storage services.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%