使用 .NET SDK 對 Data Lake Storage Gen1 進行檔案系統作業

在本文中,您會了解如何使用 .NET SDK 在 Data Lake Storage Gen1 上執行檔案系統作業。 檔案系統作業包括在 Data Lake Storage Gen1 帳戶中建立資料夾、上傳檔案、下載檔案等。

如需有關如何使用 .NET SDK 在 Data Lake Storage Gen1 上進行帳戶管理作業的指示,請參閱使用.NET SDK 在 Data Lake Storage Gen1 上進行帳戶管理作業

必要條件

建立 .NET 應用程式

GitHub 上可用的程式碼範例會逐步引導您完成在存放區中建立檔案,串連檔案、下載檔案,以及刪除存放區中一些檔案的程序。 本節文章會逐步解說程式碼的主要部分。

  1. 在 Visual Studio 中,選取 [檔案] 功能表、[新增] 及 [專案]。

  2. 選擇 [主控台應用程式 (.NET Framework)],然後選取 [下一步]。

  3. 在 [專案名稱] 中,輸入 CreateADLApplication,然後選取 [建立]

  4. 將 NuGet 套件新增至您的專案。

    1. 在方案總管中以滑鼠右鍵按一下專案名稱,然後按一下 [ 管理 NuGet 封裝]。

    2. 在 [NuGet 套件管理員] 索引標籤中,確定 [套件來源] 設為 [nuget.org],並確認已選取 [包含發行前版本] 核取方塊。

    3. 搜尋並安裝下列 NuGet 封裝:

      • Microsoft.Azure.DataLake.Store - 本文使用 v1.0.0。
      • Microsoft.Rest.ClientRuntime.Azure.Authentication - 本文使用 v2.3.1。

      關閉 [NuGet 套件管理員]。

  5. 開啟 Program.cs,刪除現有的程式碼,然後納入下列陳述式以新增命名空間的參考。

    using System;
    using System.IO;using System.Threading;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Security.Cryptography.X509Certificates; // Required only if you're using an Azure AD application created with certificates
    
    using Microsoft.Rest;
    using Microsoft.Rest.Azure.Authentication;
    using Microsoft.Azure.DataLake.Store;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    
  6. 宣告變數如下,並提供預留位置的值。 此外,請確定電腦上有此處提供的本機路徑和檔案名稱。

    namespace SdkSample
    {
        class Program
        {
            private static string _adlsg1AccountName = "<DATA-LAKE-STORAGE-GEN1-NAME>.azuredatalakestore.net";
        }
    }
    

在本文的其餘章節中,您可以了解如何使用可用的 .NET 方法來進行一些作業,例如驗證、檔案上載等。

驗證

建立用戶端物件

下列程式碼片段會建立 Data Lake Storage Gen1 檔案系統用戶端物件,以便對服務發出要求。

// Create client objects
AdlsClient client = AdlsClient.CreateClient(_adlsg1AccountName, adlCreds);

建立檔案和目錄

將下列程式碼片段新增至應用程式。 這個程式碼片段會新增檔案與任何不存在的父目錄。

// Create a file - automatically creates any parent directories that don't exist
// The AdlsOutputStream preserves record boundaries - it does not break records while writing to the store

using (var stream = client.CreateFile(fileName, IfExists.Overwrite))
{
    byte[] textByteArray = Encoding.UTF8.GetBytes("This is test data to write.\r\n");
    stream.Write(textByteArray, 0, textByteArray.Length);

    textByteArray = Encoding.UTF8.GetBytes("This is the second line.\r\n");
    stream.Write(textByteArray, 0, textByteArray.Length);
}

附加到檔案

下列程式碼片段會將資料附加至 Data Lake Storage Gen1 帳戶中的現有檔案。

// Append to existing file

using (var stream = client.GetAppendStream(fileName))
{
    byte[] textByteArray = Encoding.UTF8.GetBytes("This is the added line.\r\n");
    stream.Write(textByteArray, 0, textByteArray.Length);
}

讀取檔案

下列程式碼片段會讀取 Data Lake Storage Gen1 中的檔案內容。

//Read file contents

using (var readStream = new StreamReader(client.GetReadStream(fileName)))
{
    string line;
    while ((line = readStream.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

取得檔案屬性

下列程式碼片段會傳回與檔案或目錄相關聯的屬性。

// Get file properties
var directoryEntry = client.GetDirectoryEntry(fileName);
PrintDirectoryEntry(directoryEntry);

PrintDirectoryEntry 方法的定義包含在 Github 上的範例中。

重新命名檔案

下列程式碼片段會重新命名 Data Lake Storage Gen1 帳戶中的現有檔案。

// Rename a file
string destFilePath = "/Test/testRenameDest3.txt";
client.Rename(fileName, destFilePath, true);

列舉目錄

下列程式碼片段會列舉 Data Lake Storage Gen1 帳戶中的目錄。

// Enumerate directory
foreach (var entry in client.EnumerateDirectory("/Test"))
{
    PrintDirectoryEntry(entry);
}

PrintDirectoryEntry 方法的定義包含在 Github 上的範例中。

以遞迴方式刪除目錄

下列程式碼片段會以遞迴方式刪除目錄,以及其所有子目錄。

// Delete a directory and all its subdirectories and files
client.DeleteRecursive("/Test");

範例

以下是幾個可顯示如何使用 Data Lake Storage Gen1 Filesystem SDK 的範例。

另請參閱

下一步