如何使用.NET API创建ADLS帐户

时间:2019-01-15 21:11:18

标签: c# azure azure-data-lake

任何人都可以共享它,以使用C#代码(不是powershell,az cli等)在Azure订阅中以编程方式创建ADLS gen1数据湖吗?

我遇到过这样的文档-https://docs.microsoft.com/en-us/dotnet/api/overview/azure/data-lake-store?view=azure-dotnet-假设湖泊已经存在

1 个答案:

答案 0 :(得分:0)

有关详细步骤,请参阅此article

注意

1。对于account name,请勿使用后缀.azuredatalakestore.net

2。对于身份验证,我使用service-to-service authentication。它将使用TENANT_id(AAD中的目录ID),CLIENTID(AAD中的应用程序ID)和secret_key,您可以通过遵循此article来获取它们。

3。按照官方文档中的说明安装以下版本程序包(可以使用其他版本,但需要进行一些小的更改):

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门户中检查新创建的帐户:

enter image description here