Montare un bucket Cloud Storage utilizzando Cloud Storage FUSE

Scopri come montare un bucket Cloud Storage come file system locale utilizzando Cloud Storage FUSE, in modo da poter interagire con gli oggetti utilizzando la semantica standard del file system.

In questa guida rapida completerai le seguenti attività:

  • Installare Cloud Storage FUSE su Debian o Ubuntu

  • Monta il bucket come cartella sul computer

  • Carica un oggetto nel bucket

Utilizzerai Google Cloud CLI per creare e gestire l'oggetto nel bucket di archiviazione e gcsfuse CLI per montare il bucket sulla macchina.

Prima di iniziare

Per configurare un progetto, completa i seguenti passaggi:

  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.

      Vai a IAM
    2. Seleziona il progetto.
    3. Fai clic su Concedi l'accesso.
    4. Nel campo Nuove entità, inserisci il tuo identificatore utente. In genere si tratta dell'indirizzo email di un Account Google.

    5. Nell'elenco Seleziona un ruolo, seleziona un ruolo.
    6. Per concedere altri ruoli, fai clic su Aggiungi un altro ruolo e aggiungi ogni ruolo aggiuntivo.
    7. Fai clic su Salva.
  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.

      Vai a IAM
    2. Seleziona il progetto.
    3. Fai clic su Concedi l'accesso.
    4. Nel campo Nuove entità, inserisci il tuo identificatore utente. In genere si tratta dell'indirizzo email di un Account Google.

    5. Nell'elenco Seleziona un ruolo, seleziona un ruolo.
    6. Per concedere altri ruoli, fai clic su Aggiungi un altro ruolo e aggiungi ogni ruolo aggiuntivo.
    7. Fai clic su Salva.
  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. Avere un bucket Cloud Storage da montare. Se non ne hai già uno, crea un nuovo bucket.
  15. Installa Cloud Storage FUSE

    Per installare Cloud Storage FUSE, completa le seguenti istruzioni utilizzando la shell locale su Debian o Ubuntu:

    1. Aggiungi l'URL di distribuzione di Cloud Storage FUSE come origine pacchetto:

      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. Importa la chiave pubblica del repository APT: Google Cloud

      curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    3. Aggiorna l'elenco dei pacchetti disponibili:

      sudo apt-get update
    4. Installa Cloud Storage FUSE e la relativa dipendenza FUSE:

      sudo apt-get install fuse gcsfuse
    5. Quando richiesto, premi Invio per confermare.

    6. Verifica che Cloud Storage FUSE sia stato installato:

      gcsfuse -v

      In caso di esito positivo, il comando restituisce un output simile al seguente:

      gcsfuse version 0.41.12 (Go version go1.18.4)

    Monta il secchio

    Per montare il bucket sul file system locale, completa i seguenti passaggi:

    1. Genera le credenziali predefinite dell'applicazione utilizzando il comando gcloud auth application-default login:

      gcloud auth application-default login

      Cloud Storage FUSE carica automaticamente le credenziali.

    2. Crea una directory in cui montare il bucket di archiviazione:

      mkdir "$HOME/mount-folder"
    3. Monta il bucket di archiviazione utilizzando il comando gcsfuse:

      gcsfuse BUCKET_NAME "$HOME/mount-folder"

      Sostituisci BUCKET_NAME con il nome del bucket che vuoi montare.

      In caso di esito positivo, il comando restituisce un output contenente un testo simile al seguente:

      File system has been successfully mounted.

    Carica un oggetto nel bucket

    Per scaricare un oggetto di esempio e caricarlo nel bucket montato, completa i seguenti passaggi:

    Immagine di un gattino da caricare nel bucket.

    1. Fai clic con il tasto destro del mouse sull'immagine e salvala in una posizione del computer, ad esempio sul desktop.

      Puoi anche scaricare l'immagine utilizzando il comando cURL curl -O https://cloud.google.com/storage/images/kitten.png.

    2. Copia l'immagine dalla posizione in cui è salvata nella cartella in cui è montato il bucket utilizzando il comando cp:

      cp kitten.png "$HOME/mount-folder/kitten.png"
    3. Verifica che l'immagine sia stata copiata nel file system locale e caricata nel bucket seguendo i passaggi:

      1. Per verificare che kitten.png sia stato copiato nel file system locale, esegui ls nella cartella in cui è montato il bucket:

        ls "$HOME/mount-folder"

        Se l'operazione di copia è riuscita, kitten.png viene restituito nell'output.

      2. Per elencare i contenuti del bucket, utilizza il comando gcloud storage ls:

        gcloud storage ls gs://BUCKET_NAME

        Sostituisci BUCKET_NAME con il nome del tuo bucket.

        Se l'immagine è stata caricata correttamente nel bucket, nell'output viene restituito kitten.png.

    Esegui la pulizia

    Per evitare che al tuo account Google Cloud vengano addebitati costi relativi alle risorse utilizzate in questa pagina, elimina le risorse completando i seguenti passaggi:

    1. Smonta il bucket di archiviazione dalla macchina Linux utilizzando lo strumento fusermount:

      fusermount -u "$HOME/mount-folder"
    2. Per eliminare il bucket e tutto ciò che contiene al suo interno, utilizza il comando gcloud storage rm con il flag --recursive:

      gcloud storage rm gs://BUCKET_NAME --recursive

      Sostituisci BUCKET_NAME con il nome del tuo bucket.

      In caso di esito positivo, il comando restituisce un output simile al seguente:

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

    Passaggi successivi