使用 Cloud Storage FUSE 掛接 Cloud Storage bucket

掛接選項

本頁面說明如何使用 Cloud Storage FUSE,透過幾種方式將 Cloud Storage bucket 掛接到本機檔案系統。

事前準備

如要掛接及存取 bucket,請先完成下列工作。

取得掛接及存取值區所需的角色

如要在值區中掛接、存取及執行讀取和寫入作業,請要求值區擁有者授予您值區的 Storage Object User (roles/storage.objectUser) 角色。

這個預先定義的角色具備從已掛接值區讀取資料,以及寫入已掛接值區的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

  • storage.objects.create
  • storage.objects.delete
  • storage.objects.get
  • storage.objects.list
  • storage.objects.update

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

如需如何授予值區角色的操作說明,請參閱「搭配值區使用 IAM」。

安裝 Cloud Storage FUSE

如果尚未安裝,請安裝 Cloud Storage FUSE

驗證 Cloud Storage FUSE 要求

如要驗證 Cloud Storage FUSE 對 Cloud Storage 的要求,您必須設定應用程式預設憑證。根據預設,Cloud Storage FUSE 會自動載入現有的應用程式預設憑證,不需進一步設定。

如要使用 Google Cloud CLI 設定應用程式預設憑證,請完成下列步驟:

  1. Install the Google Cloud CLI.

  2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

建立 Compute Engine 虛擬機器 (VM) 時,其服務帳戶也可驗證 Cloud Storage FUSE 的存取權。

掛接 bucket

如要將 bucket 掛接至本機檔案系統,請使用下列指令:

gcsfuse GLOBAL_OPTIONS BUCKET_NAME MOUNT_POINT

其中:

  • GLOBAL_OPTIONS 是您可以加入的選項,用來控管掛接設定方式。如要進一步瞭解選項和使用方式,請參閱 Cloud Storage FUSE CLI 選項

  • BUCKET_NAME 是要掛接的值區名稱。例如,my-bucket。如要執行動態掛接,請從指令中排除值區名稱。

  • MOUNT_POINT 是要掛接 bucket 的本機目錄。例如:/path/to/mount/point

Cloud Storage FUSE 結束後,您可以在 bucket 掛接點執行 ls,存取已掛接的 bucket。如果希望 Cloud Storage FUSE 留在前景 (例如用於偵錯記錄),可以執行 gcsfuse 指令並加上 --foreground 標記。

安裝範例

本節說明掛接值區的各種範例指令。

靜態掛接

靜態掛接是指掛接特定 bucket。舉例來說,如要將名為 my-bucket 的 bucket 掛接到 /path/to/mount/point 目錄,請執行下列指令:

mkdir /path/to/mount/point
gcsfuse my-bucket /path/to/mount/point

動態掛接

動態掛接是指將使用者有權存取的所有 bucket 掛接為子目錄。

執行動態掛接時,請從指令中排除 bucket 名稱。 舉例來說,假設您有權存取名為 my-bucket-1my-bucket-2my-bucket-3 的值區。如要將所有 buckets 掛接到 /path/to/mount/point 目錄,請執行下列指令:

mkdir /path/to/mount/point
gcsfuse /path/to/mount/point

接著,您就可以將 bucket 存取為子目錄:

ls /path/to/mount/point/my-bucket-1/
ls /path/to/mount/point/my-bucket-2/
ls /path/to/mount/point/my-bucket-3/

無法從根掛接點列出動態掛接的儲存空間。而是必須在清單作業中指定值區名稱。

以唯讀模式掛接值區

如要以唯讀模式掛接值區,請將 -o ro 選項傳遞至 gcsfuse 指令。舉例來說,如要將名為 my-bucket 的 bucket 以唯讀模式掛接至 /path/to/mount/point 目錄,請執行下列指令:

gcsfuse -o ro my-bucket  /path/to/mount/point

掛接 bucket 內的目錄

根據預設,Cloud Storage FUSE 會完整掛接值區,包括值區的所有內容和目錄結構。如要在 bucket 中掛接特定目錄,請將 --only-dir 選項傳遞至 gcsfuse 指令。舉例來說,如要將 my-bucket/a/b 目錄掛接到 /path/to/mount/point 目錄,請執行下列指令:

gcsfuse --only-dir a/b my-bucket /path/to/mount/point

使用 Linux mount 指令掛接

Cloud Storage FUSE 安裝作業包含 Linux mount 指令可辨識的輔助程式,可讓您使用 mount 指令掛接值區。如果您使用 Linux,必須從根目錄執行 mount 指令。舉例來說,下列指令會將名為 my-bucket 的值區掛接至 /path/to/mount/point 路徑:

sudo mount -t gcsfuse -o rw,user my-bucket /path/to/mount/point

使用 Linux mount 指令掛接時,Cloud Storage FUSE CLI 選項可做為 -o 旗標的引數傳遞,但其中的連字號 (-) 應替換為底線 (_)。例如 implicit_dirs,而非 implicit-dirs

sudo mount -t gcsfuse -o implicit_dirs my-bucket /path/to/mount/point

永久掛接

Cloud Storage FUSE 安裝作業會將輔助程式安裝到系統的 /sbin/mount.gcsfuse 路徑。這個輔助程式可讓您使用 /etc/fstab 檔案建立掛接點,以便保留掛接點。

舉例來說,在 /etc/fstab 檔案中新增下列程式碼時,系統會為路徑 /mount/point 中名為 my-bucket 的 bucket 定義掛接點:

my-bucket /mount/point gcsfuse rw,noauto,user

其中:

  • rw 選項會指定建立具有讀取和寫入權限的掛接點。

  • noauto 選項會指定檔案系統不應在開機時掛接。

  • user 選項可讓任何人建立 /etc/fstab 檔案中指定的掛接點。將範例行新增至 /etc/fstab 檔案後,您就能以非根使用者身分執行 mount /mount/point

如要在開機時自動掛接值區,您可能需要在 /etc/fstab 項目中加入 x-systemd.requires=network-online.target 選項或 _netdev 選項,確保 Cloud Storage FUSE 會等待網路系統準備就緒,再進行掛接。例如:

my-bucket /mount/point gcsfuse rw,x-systemd.requires=network-online.target,user

您可以在 /etc/fstab 項目中加入 uidgid 或這兩項選項,以特定非根使用者身分自動掛接儲存空間。例如:

my-bucket /mount/point gcsfuse rw,_netdev,allow_other,uid=1001,gid=1001

linux mount 指令類似,在 /etc/fstab 檔案中用於掛接的標記必須使用底線 (_) 而非連字號 (-)。舉例來說,請使用 implicit_dirs 而非 implicit-dirs

掛接含有資料夾的值區

您可以掛接多個資料夾類型的值區。

掛接含有代管資料夾的 Bucket

如要掛接包含受管理資料夾的 bucket,請在指令中加入 --implicit-dirs 選項。

舉例來說,如要將名為 my-bucket 的值區及其代管資料夾掛接到 /path/to/mount/point 目錄,請執行下列指令:

gcsfuse --implicit-dirs my-bucket /path/to/mount/point

使用受管理資料夾掛接 bucket 時,請注意下列限制和考量事項:

  • 掛接值區不會顯示空白的受管理資料夾。

  • 您無法使用 Cloud Storage FUSE 設定或管理代管資料夾的 IAM 權限。如要在代管資料夾中設定或管理 IAM 權限,請參閱「搭配代管資料夾使用 IAM」。

  • 您可以使用 cpmv 指令,複製及移動已掛接儲存空間中的受管理資料夾。複製或移動受管理的資料夾時,原始受管理資料夾的身分與存取權管理政策不會保留到新的受管理資料夾。您必須在新受管理資料夾中設定新的 IAM 政策。

掛接具有隱含定義資料夾的 bucket

您可以在 Cloud Storage FUSE CLI 中使用 --implicit-dirs 設定選項,掛接隱含定義的資料夾。隱含定義的資料夾是指未明確建立為 Cloud Storage 物件的資料夾。--implicit-dirs 設定選項可讓您在掛接值區時查看資料夾。

舉例來說,如要將名為 my-bucket 的值區掛接到 /path/to/mount/point 目錄,並隱含定義資料夾,請執行下列指令:

gcsfuse --implicit-dirs my-bucket /path/to/mount/point

掛接啟用階層命名空間的 bucket 和資料夾

您可以掛接啟用階層結構式命名空間的值區,包括其中的資料夾。使用階層式命名空間掛接 bucket 時,不必指定 --implicit-dirs 設定選項,掛接的 bucket 中就會顯示資料夾。

舉例來說,如要將名為 my-bucket 的 bucket 連同資料夾掛接到 /path/to/mount/point 目錄,請執行下列指令:

gcsfuse my-bucket /path/to/mount/point

控管掛接點的存取權限

根據預設,Cloud Storage FUSE 會建立值區掛接點,並設定權限,只允許叫用使用者存取已掛接值區的內容。這是 FUSE 核心內建的安全措施。

如要掛接值區並允許其他人存取值區掛接點,您可以執行 mount 指令,並搭配 -o allow_other 選項:

mount -t gcsfuse -o allow_other my-bucket /path/to/mount/point

如要避免將儲存空間掛接為根目錄,請在 /etc/fuse.conf 檔案中新增 user_allow_other,允許其他使用者存取儲存空間掛接點。

卸載 bucket

如要卸載 bucket,請使用 fusermount 工具:

fusermount -u /path/to/mount/point

後續步驟