在 Linux 掛載 Azure Blob Storage — Ubuntu 版 NetDrive

閱讀時間 6 分鐘 provider-guide azure linux
Casey
CaseyProduct Manager
使用 NetDrive 的實驗性 Ubuntu 版本,將 Azure Blob Storage 容器掛載為 Linux 掛載點——設定、驗證與驗證步驟。

某資料工程團隊每晚將報表檔案匯出到 Azure Blob Storage 容器,而負責重新處理這些檔案的 Ubuntu 主機不該為了讀取一個檔案,就在每支腳本裡塞入 Azure SDK。團隊原本就有 cpfind 以及一堆能正常對本機路徑運作的 shell pipeline。NetDrive 的 Linux 版本會將容器掛載成其中一個本機路徑,讓既有腳本不必改動就能繼續運作。

NetDrive drive manager showing Google Drive, S3 and pCloud mounted as drive lettersMounted clouds appearing as native drives in Windows File Explorer

在 Ubuntu 掛載 Azure Blob Storage

NetDrive 讓 Google Drive、OneDrive、S3、SFTP、WebDAV 等在 Windows 與 macOS 上顯示為原生磁碟機 — 不需同步,也不需完整下載。

  • 運行於 NetDrive 實驗性 Ubuntu 版本
  • 既有 shell 腳本可透過一般掛載點讀寫
  • 背景上傳,大型檔案不會卡住呼叫端行程
WindowsmacOS
下載 Linux 版 NetDrive →

免費試用。提供終身與訂閱方案。

為什麼掛載勝過直接呼叫 Blob API

直接與 Azure Blob Storage 溝通,意味著要引入 Azure SDK 或 az storage blob CLI 指令、處理 SAS token 過期,還要用作業所使用的任何語言自行寫重試邏輯。單一腳本這樣做還算可以應付,但一旦十幾個 cron 作業和 CI 步驟都各自需要一套相同的配管,問題就會累積起來。掛載容器就消除了這一層——任何原本就能讀寫本機路徑的程式,都能以同樣方式存取掛載點。

NetDrive 自 3.11.204 版(2020-07-22)起新增 Azure Blob Storage 支援,與新增 Azure File Storage 的版本相同。在 Linux 上,這個連線類型運行於 NetDrive 的實驗性 Ubuntu 版本,而非測試更為完整的 Windows 與 macOS 版本——在把正式環境的 pipeline 指向它之前值得先了解這一點,因為 Linux 在 NetDrive 自己的比較頁面上標示為「實驗性」。

Azure Blob Storage provider logo — NetDrive supports Azure Blob Storage on Windows, macOS, and Linux

你需要從 Azure Portal 取得的資訊

在開啟 NetDrive 之前,先收集三項資訊:

  • 儲存體帳戶名稱——位於 Azure Portal 中你的儲存體帳戶資源概觀頁面上。
  • 容器名稱——要掛載的特定 Blob 容器,例如 nightly-exports
  • 驗證憑證——可以是主要帳戶金鑰(Azure Portal → Storage account → Security + networkingAccess keys),也可以是限定該容器範圍的共用存取簽章(SAS)token。對於無人值守的主機來說,過期時間與你的憑證輪替政策一致的 SAS token 是較安全的選擇。

你還需要 Ubuntu 16.04 或更新版本,並確保主機防火牆已開放對 Azure Storage 端點的對外連線。

在 Ubuntu 上安裝 NetDrive

NetDrive for Linux 不在 Ubuntu 的預設套件庫中,因此設定流程是透過 GitHub,而不是 apt:

  1. 前往 github.com/NetDrive/installer,下載對應你 Ubuntu 版本的發行版。
  2. 安裝前先檢查該版本的發行說明——驅動元件必須先載入,否則連線無法掛載。
  3. 安裝套件,然後啟動一次 NetDrive 讓驅動載入。依主機設定不同,此步驟可能會要求輸入 sudo

逐步操作:連接 Azure Blob 容器

  1. 開啟 NetDrive 的 Drive Manager,點選 + Add Drive
  2. 從供應商清單選擇 Azure Blob Storage

NetDrive Drive Manager showing multiple cloud connections available to add

  1. 輸入從 Azure Portal 取得的帳戶名稱容器
  2. 將帳戶金鑰或 SAS token 貼入驗證欄位。
  3. 設定掛載點,例如 /mnt/azure-exports,然後點選 Connect

在讓工作依賴掛載之前先驗證

在讓無人值守的工作依賴這個掛載點之前,先確認掛載確實生效:

# Confirm the mount is listed
mount | grep azure-exports

# Or just list it
ls /mnt/azure-exports

Confirming that a connected drive mounted successfully after setup

如果目錄顯示為空,或掛載點始終沒有出現,先檢查主機防火牆是否允許對外連線到 Azure Storage 端點,再去懷疑憑證有誤——在鎖定嚴格的建置伺服器上,被擋住的連接埠比錯誤的 SAS token 更常是問題所在。

Checking that a network port is reachable when troubleshooting a failed connection

在 Linux 版本上要留意的事

  • 獨立的發行節奏。 Windows 或 macOS 上釋出的修正,不會自動在同一週內同步到 Linux 版本。
  • 沒有 apt 整合。 更新來自 GitHub 發行版,而非套件管理員,因此追蹤新版本需要手動進行。
  • Windows 與 macOS 的測試更為完整。 讀取 Azure Blob Storage 的夜間匯出作業,適合交給 Linux 版本處理;但在把無人值守的正式環境 pipeline 交給它之前,務必先充分測試。

總結

在 Ubuntu 上掛載 Azure Blob Storage 容器,能讓 Linux 主機獲得與 NetDrive 在 Windows 與 macOS 上相同的磁碟機式存取體驗,而不必為了 Azure SDK 重寫腳本。若要在測試更完整的平台上使用同一個供應商,請參閱在 Windows 掛載 Azure Blob Storage在 macOS 掛載 Azure Blob Storage;若想了解更全面的 Linux 相關內容,請參閱 Ubuntu Linux 上的 NetDrive

— Casey, NetDrive