Cloud Storage FUSE を使用して Cloud Storage バケットをマウントする

Cloud Storage FUSE を使用して Cloud Storage バケットをローカル ファイル システムとしてマウントする方法を確認し、標準のファイル システムのセマンティクスを使用してオブジェクトを操作できるようにします。

このクイックスタートでは、次のタスクを行います。

  • Debian または Ubuntu に Cloud Storage FUSE をインストールする

  • バケットをマシン上のフォルダとしてマウントする

  • バケットにオブジェクトをアップロードする

Google Cloud CLI を使用して、ストレージ バケット内のオブジェクトの作成と管理を行います。また、gcsfuse CLI を使用してマシンにバケットをマウントします。

始める前に

プロジェクトを設定するには、次の操作を行います。

  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. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  5. Install the Google Cloud CLI.

  6. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して gcloud CLI にログインする必要があります。

  7. gcloud CLI を初期化するには、次のコマンドを実行します。

    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. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  11. Install the Google Cloud CLI.

  12. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して gcloud CLI にログインする必要があります。

  13. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  14. マウントする Cloud Storage バケットを用意します。まだバケットを保有していない場合は、新しいバケットを作成します。
  15. Cloud Storage FUSE をインストールする

    Cloud Storage FUSE をインストールするには、Cloud Storage FUSE をインストールするの手順に沿って操作します。

    バケットをマウントする

    バケットをローカル ファイル システムにマウントするには、次の手順を行います。

    1. gcloud auth application-default login コマンドを使用してアプリケーションのデフォルト認証情報を生成します。

      gcloud auth application-default login

      Cloud Storage FUSE が認証情報を自動的に読み込みます。

    2. ストレージ バケットをマウントするディレクトリを作成します。

      mkdir "$HOME/mount-folder"
    3. gcsfuse コマンドを使用してストレージ バケットをマウントします。

      gcsfuse BUCKET_NAME "$HOME/mount-folder"

      BUCKET_NAME は、マウントするバケットの名前に置き換えます。

      成功した場合、コマンドは次のようなテキストを含む出力を返します。

      File system has been successfully mounted.

    バケットにオブジェクトをアップロードする

    サンプル オブジェクトをダウンロードして、マウントされたバケットにアップロードするには、次の操作を行います。

    バケットにアップロードする子猫の画像。

    1. 画像を右クリックして、パソコンに保存します(デスクトップなど)。

      cURL コマンド curl -O https://cloud.google.com/storage/images/kitten.png を使用してイメージをダウンロードすることもできます。

    2. cp コマンドを使用して、保存した場所からバケットがマウントされているフォルダに画像をコピーします。

      cp kitten.png "$HOME/mount-folder/kitten.png"
    3. 次の手順に沿って、画像がローカル ファイル システムにコピーされ、バケットに正常にアップロードされたことを確認します。

      1. kitten.png がローカル ファイル システムにコピーされたことを確認するには、バケットがマウントされているフォルダで ls を実行します。

        ls "$HOME/mount-folder"

        コピー オペレーションが成功した場合は、出力で kitten.png が返されます。

      2. バケットの内容を一覧表示するには、gcloud storage ls コマンドを使用します。

        gcloud storage ls gs://BUCKET_NAME

        BUCKET_NAME は、実際のバケット名を置き換えます。

        画像がバケットに正常にアップロードされると、出力で kitten.png が返されます。

    クリーンアップ

    このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順でリソースを削除します。

    1. fusermount ツールを使用して、Linux マシンからストレージ バケットをマウント解除します。

      fusermount -u "$HOME/mount-folder"
    2. バケットとその中のすべてのものを削除するには、--recursive フラグを指定して gcloud storage rm コマンドを使用します。

      gcloud storage rm gs://BUCKET_NAME --recursive

      BUCKET_NAME は、実際のバケット名を置き換えます。

      成功した場合、コマンドは次のような出力を返します。

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

    次のステップ