使用 .NET SDK 在 Azure Data Lake Storage Gen1 上执行帐户管理作

本文介绍如何使用 .NET SDK 在 Azure Data Lake Storage Gen1 上执行帐户管理作。 帐户管理作包括创建 Data Lake Storage Gen1 帐户、列出 Azure 订阅中的帐户、删除帐户等。

有关如何使用 .NET SDK 对 Data Lake Storage Gen1 执行数据管理操作的说明文档,请参阅 使用 .NET SDK 对 Data Lake Storage Gen1 执行文件系统操作

先决条件

  • Visual Studio 2013 或更高版本。 以下说明使用的是 Visual Studio 2019。

  • Azure 订阅。 请参阅获取 Azure 免费试用版

创建 .NET 应用程序

  1. 在 Visual Studio 中,依次选择“文件”菜单、“新建”,然后选择“项目”

  2. 选择“控制台应用(.NET Framework)”,然后选择“下一步”

  3. 在“项目名称”中,输入 CreateADLApplication,然后选择“创建”

  4. 将 NuGet 包添加到项目。

    1. 在解决方案资源管理器中右键单击项目名称,单击“管理 NuGet 包”

    2. 在“NuGet 包管理器”选项卡上,确保“包源”设置为“nuget.org”,“包含预发行版”复选框处于选中状态。

    3. 搜索并安装以下 NuGet 包:

      • Microsoft.Azure.Management.DataLake.Store - 本教程使用 v2.1.3-预览版。

      • Microsoft.Rest.ClientRuntime.Azure.Authentication - 本教程使用 v2.2.12。

        添加 NuGet 源

    4. 关闭“NuGet 包管理器”。

  5. 打开“Program.cs” ,删除现有代码,并包含以下语句,添加对命名空间的引用。

    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Collections.Generic;
    using System.Security.Cryptography.X509Certificates; // Required only if you are using an Azure AD application created with certificates
    
    using Microsoft.Rest;
    using Microsoft.Rest.Azure.Authentication;
    using Microsoft.Azure.Management.DataLake.Store;
    using Microsoft.Azure.Management.DataLake.Store.Models;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    
  6. 声明变量并为占位符提供值。 此外,请确保计算机上存在提供的本地路径和文件名。

    namespace SdkSample
    {
        class Program
        {
            private static DataLakeStoreAccountManagementClient _adlsClient;
    
            private static string _adlsAccountName;
            private static string _resourceGroupName;
            private static string _location;
            private static string _subId;
    
            private static void Main(string[] args)
            {
                _adlsAccountName = "<DATA-LAKE-STORAGE-GEN1-NAME>.azuredatalakestore.net"; 
                _resourceGroupName = "<RESOURCE-GROUP-NAME>"; 
                _location = "East US 2";
                _subId = "<SUBSCRIPTION-ID>";                    
            }
        }
    }
    

本文的其余部分介绍如何使用可用的 .NET 方法执行身份验证、文件上传等作。

身份验证

创建客户端对象

以下代码片段创建 Data Lake Storage Gen1 帐户客户端对象,该对象用于向服务发出帐户管理请求,例如创建帐户、删除帐户等。

// Create client objects and set the subscription ID
_adlsClient = new DataLakeStoreAccountManagementClient(armCreds) { SubscriptionId = _subId };

创建 Data Lake Storage Gen1 帐户

在您创建 Data Lake Storage Gen1 帐户客户端对象时所提供的 Azure 订阅中,以下代码片段创建了一个 Data Lake Storage Gen1 帐户。

// Create Data Lake Storage Gen1 account
var adlsParameters = new DataLakeStoreAccount(___location: _location);
_adlsClient.Account.Create(_resourceGroupName, _adlsAccountName, adlsParameters);

列出订阅中的所有 Data Lake Storage Gen1 帐户

将以下方法添加到类定义中。 以下代码片段列出了给定 Azure 订阅中的所有 Data Lake Storage Gen1 帐户。

// List all Data Lake Storage Gen1 accounts within the subscription
public static List<DataLakeStoreAccountBasic> ListAdlStoreAccounts()
{
    var response = _adlsClient.Account.List(_adlsAccountName);
    var accounts = new List<DataLakeStoreAccountBasic>(response);

    while (response.NextPageLink != null)
    {
        response = _adlsClient.Account.ListNext(response.NextPageLink);
        accounts.AddRange(response);
    }

    return accounts;
}

删除 Data Lake Storage Gen1 帐户

以下代码片段删除之前创建的 Data Lake Storage Gen1 帐户。

// Delete Data Lake Storage Gen1 account
_adlsClient.Account.Delete(_resourceGroupName, _adlsAccountName);

另请参阅

后续步骤