使用 Cloud Storage FUSE 掛接 Cloud Storage bucket

瞭解如何使用 Cloud Storage FUSE 將 Cloud Storage 值區掛接為本機檔案系統,以便使用標準檔案系統語意與物件互動。

在本快速入門導覽課程中,您將完成下列工作:

  • 在 Debian 或 Ubuntu 上安裝 Cloud Storage FUSE

  • 將 bucket 掛接為機器上的資料夾

  • 將物件上傳至 bucket

您將使用 Google Cloud CLI 建立及管理儲存空間 bucket 中的物件,並使用 gcsfuse CLI 將 bucket 掛接至機器。

事前準備

如要設定專案,請完成下列步驟:

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Make sure that you have the following role or roles on the project: Storage Admin (roles/storage.admin), Storage Bucket Viewer (roles/storage.bucketViewer)

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  5. Install the Google Cloud CLI.

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

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

    gcloud init
  8. In the Google Cloud console, on the project selector page, click Create project to begin creating a new Google Cloud project.

    Go to project selector

  9. Make sure that billing is enabled for your Google Cloud project.

  10. Make sure that you have the following role or roles on the project: Storage Admin (roles/storage.admin), Storage Bucket Viewer (roles/storage.bucketViewer)

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  11. Install the Google Cloud CLI.

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

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

    gcloud init
  14. 擁有要掛接的 Cloud Storage bucket。如果沒有,請建立新值區
  15. 安裝 Cloud Storage FUSE

    如要安裝 Cloud Storage FUSE,請在 Debian 或 Ubuntu 上使用本機殼層,完成下列操作說明:

    1. 將 Cloud Storage FUSE 發布作業網址新增為套件來源:

      export GCSFUSE_REPO=gcsfuse-`lsb_release -c -s`
      echo "deb https://packages.cloud.google.com/apt $GCSFUSE_REPO main" | sudo tee /etc/apt/sources.list.d/gcsfuse.list
    2. 匯入 Google Cloud APT 存放區公開金鑰:

      curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    3. 更新可用套件清單:

      sudo apt-get update
    4. 安裝 Cloud Storage FUSE 及其依附元件 FUSE:

      sudo apt-get install fuse gcsfuse
    5. 系統提示時,請按下 Enter 鍵確認。

    6. 確認 Cloud Storage FUSE 已安裝:

      gcsfuse -v

      如果成功,指令會傳回類似以下的輸出內容:

      gcsfuse version 0.41.12 (Go version go1.18.4)

    掛接 bucket

    如要將 bucket 掛接到本機檔案系統,請完成下列步驟:

    1. 使用 gcloud auth application-default login 指令產生應用程式預設憑證:

      gcloud auth application-default login

      Cloud Storage FUSE 會自動載入憑證。

    2. 建立要掛接儲存空間值區的目錄:

      mkdir "$HOME/mount-folder"
    3. 使用 gcsfuse 指令掛接儲存空間 bucket:

      gcsfuse BUCKET_NAME "$HOME/mount-folder"

      BUCKET_NAME 替換為要掛接的值區名稱。

      如果成功,指令會傳回類似下列文字的輸出內容:

      File system has been successfully mounted.

    將物件上傳至 bucket

    如要下載範例物件並上傳至已掛接的值區,請完成下列步驟:

    要上傳到 bucket 的小貓圖片。

    1. 在圖片上按一下滑鼠右鍵,並將圖片儲存至電腦的任意位置 (例如桌面)。

      您也可以使用 cURL 指令 curl -O https://cloud.google.com/storage/images/kitten.png 下載映像檔。

    2. 使用 cp 指令,將圖片從儲存位置複製到已掛接 bucket 的資料夾:

      cp kitten.png "$HOME/mount-folder/kitten.png"
    3. 請按照下列步驟,確認圖片已複製到本機檔案系統,並成功上傳至值區:

      1. 如要確認 kitten.png 已複製到本機檔案系統,請在值區掛接的資料夾上執行 ls

        ls "$HOME/mount-folder"

        如果複製作業成功,輸出內容會傳回 kitten.png

      2. 如要列出 bucket 的內容,請使用 gcloud storage ls 指令:

        gcloud storage ls gs://BUCKET_NAME

        BUCKET_NAME 替換為您的 bucket 名稱。

        如果圖片已成功上傳至 bucket,輸出內容會傳回 kitten.png

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟刪除資源:

    1. 使用 fusermount 工具,從 Linux 電腦卸載儲存空間 bucket:

      fusermount -u "$HOME/mount-folder"
    2. 如要刪除值區及其中的所有內容,請使用帶有 --recursive 標記的 gcloud storage rm 指令:

      gcloud storage rm gs://BUCKET_NAME --recursive

      BUCKET_NAME 替換為您的 bucket 名稱。

      如果成功,指令會傳回類似以下的輸出內容:

      Removing gs://my-bucket/kitten.png#1676488739323620...
       Removing gs://my-bucket/...

    後續步驟