如何使用 .NET API 创建 ADLS 帐户
How can you create an ADLS account using .NET APIs
任何人都可以共享它以使用 C# 代码(不是 powershell、az cli 等)在 Azure 订阅中以编程方式创建 ADLS gen1 数据湖吗?
我遇到过这样的文档 - https://docs.microsoft.com/en-us/dotnet/api/overview/azure/data-lake-store?view=azure-dotnet - 这假定该湖已经存在
详细步骤请参考这篇article
注:
1.For account name
,不要使用后缀.azuredatalakestore.net
2.For认证,我用的是service-to-service authentication. It will use TENANT_id(directory id in AAD),CLIENTID(application id in AAD), and secret_key, you can get them by following this article.
3.Install官方文档中提到的以下版本包(可以使用其他版本,但需要做一些小的改动):
Microsoft.Azure.Management.DataLake.Store - This tutorial uses v2.1.3-preview.
Microsoft.Rest.ClientRuntime.Azure.Authentication - This tutorial uses v2.2.12.
示例代码:
class Program
{
private static DataLakeStoreAccountManagementClient _adlsClient;
private static string _adlsAccountName;
private static string _resourceGroupName;
private static string _location;
private static string _subId;
static void Main(string[] args)
{
_adlsAccountName = "test333";
_resourceGroupName = "ivanrg";
_location = "East US 2";
_subId = "xxxx";
string TENANT = "xxxx";
string CLIENTID = "xxxx";
System.Uri ARM_TOKEN_AUDIENCE = new System.Uri(@"https://management.core.windows.net/");
System.Uri ADL_TOKEN_AUDIENCE = new System.Uri(@"https://datalake.azure.net/");
string secret_key = "xxxx";
var armCreds = GetCreds_SPI_SecretKey(TENANT, ARM_TOKEN_AUDIENCE, CLIENTID, secret_key);
// Create client objects and set the subscription ID
_adlsClient = new DataLakeStoreAccountManagementClient(armCreds) { SubscriptionId = _subId };
// Create Data Lake Storage Gen1 account
var adlsParameters = new DataLakeStoreAccount(location: _location);
_adlsClient.Account.Create(_resourceGroupName, _adlsAccountName, adlsParameters);
Console.WriteLine("--completed--");
Console.ReadLine();
}
private static ServiceClientCredentials GetCreds_SPI_SecretKey(string tenant, Uri tokenAudience, string clientId, string secretKey)
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
var serviceSettings = ActiveDirectoryServiceSettings.Azure;
serviceSettings.TokenAudience = tokenAudience;
var creds = ApplicationTokenProvider.LoginSilentAsync(
tenant,
clientId,
secretKey,
serviceSettings).GetAwaiter().GetResult();
return creds;
}
}
查看azure portal中新建的账号:
任何人都可以共享它以使用 C# 代码(不是 powershell、az cli 等)在 Azure 订阅中以编程方式创建 ADLS gen1 数据湖吗?
我遇到过这样的文档 - https://docs.microsoft.com/en-us/dotnet/api/overview/azure/data-lake-store?view=azure-dotnet - 这假定该湖已经存在
详细步骤请参考这篇article
注:
1.For account name
,不要使用后缀.azuredatalakestore.net
2.For认证,我用的是service-to-service authentication. It will use TENANT_id(directory id in AAD),CLIENTID(application id in AAD), and secret_key, you can get them by following this article.
3.Install官方文档中提到的以下版本包(可以使用其他版本,但需要做一些小的改动):
Microsoft.Azure.Management.DataLake.Store - This tutorial uses v2.1.3-preview.
Microsoft.Rest.ClientRuntime.Azure.Authentication - This tutorial uses v2.2.12.
示例代码:
class Program
{
private static DataLakeStoreAccountManagementClient _adlsClient;
private static string _adlsAccountName;
private static string _resourceGroupName;
private static string _location;
private static string _subId;
static void Main(string[] args)
{
_adlsAccountName = "test333";
_resourceGroupName = "ivanrg";
_location = "East US 2";
_subId = "xxxx";
string TENANT = "xxxx";
string CLIENTID = "xxxx";
System.Uri ARM_TOKEN_AUDIENCE = new System.Uri(@"https://management.core.windows.net/");
System.Uri ADL_TOKEN_AUDIENCE = new System.Uri(@"https://datalake.azure.net/");
string secret_key = "xxxx";
var armCreds = GetCreds_SPI_SecretKey(TENANT, ARM_TOKEN_AUDIENCE, CLIENTID, secret_key);
// Create client objects and set the subscription ID
_adlsClient = new DataLakeStoreAccountManagementClient(armCreds) { SubscriptionId = _subId };
// Create Data Lake Storage Gen1 account
var adlsParameters = new DataLakeStoreAccount(location: _location);
_adlsClient.Account.Create(_resourceGroupName, _adlsAccountName, adlsParameters);
Console.WriteLine("--completed--");
Console.ReadLine();
}
private static ServiceClientCredentials GetCreds_SPI_SecretKey(string tenant, Uri tokenAudience, string clientId, string secretKey)
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
var serviceSettings = ActiveDirectoryServiceSettings.Azure;
serviceSettings.TokenAudience = tokenAudience;
var creds = ApplicationTokenProvider.LoginSilentAsync(
tenant,
clientId,
secretKey,
serviceSettings).GetAwaiter().GetResult();
return creds;
}
}
查看azure portal中新建的账号: