Crea un riferimento Cloud Storage con Cloud Storage for C++

I tuoi file vengono archiviati in un bucket Cloud Storage . I file in questo bucket sono presentati in una struttura gerarchica, proprio come il file system sul disco rigido locale o i dati nel Firebase Realtime Database. Creando un riferimento a un file, la tua app può accedervi. Questi riferimenti possono quindi essere utilizzati per caricare o scaricare dati, ottenere o aggiornare metadati o eliminare il file. Un riferimento può puntare a un file specifico o a un nodo di livello superiore nella gerarchia.

Se hai utilizzato Firebase Realtime Database , questi percorsi dovrebbero sembrarti molto familiari. Tuttavia, i dati dei tuoi file vengono archiviati in Cloud Storage, non nel Realtime Database.

Crea un riferimento

Crea un riferimento per caricare, scaricare o eliminare un file oppure per ottenere o aggiornare i relativi metadati. Un riferimento può essere considerato come un puntatore a un file nel cloud. I riferimenti sono leggeri, quindi puoi crearne quanti ne hai bisogno. Sono inoltre riutilizzabili per più operazioni.

I riferimenti vengono creati dal servizio storage sulla tua app Firebase chiamando il metodo GetReferenceFromUrl() e passando un URL nel formato gs://<your-cloud-storage-bucket> . Puoi trovare questo URL nella sezione Archiviazione della console Firebase .

// Get a reference to the storage service, using the default Firebase App
Storage* storage = Storage::GetInstance(app);

// Create a Cloud Storage reference from our storage service
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

Puoi creare un riferimento a una posizione più in basso nell'albero, ad esempio 'images/space.jpg' , utilizzando il metodo child su un riferimento esistente.

// Create a child reference
// images_ref now points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Child references can also take paths delimited by '/'
// space_ref now points to "images/space.jpg"
// images_ref still points to "images"
StorageReference space_ref = storage_ref.Child("images/space.jpg");

// This is equivalent to creating the full reference
StorageReference space_ref = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>/images/space.jpg");

Puoi anche utilizzare i metodi Parent e Root per navigare nella nostra gerarchia di file. Parent sale di un livello, mentre Root arriva fino in cima.

// Parent allows us to move to the parent of a reference
// images_ref now points to 'images'
StorageReference images_ref = space_ref.Parent();

// Root allows us to move all the way back to the top of our bucket
// root_ref now points to the root
StorageReference root_ref = space_ref.Root();

Child , Parent e Root possono essere concatenati più volte, poiché ciascuno restituisce un riferimento. L'eccezione è Parent of Root , che è un StorageReference non valido.

// References can be chained together multiple times
// earth_ref points to "images/earth.jpg"
StorageReference earth_ref = space_ref.Parent().Child("earth.jpg");

// null_ref is null, since the parent of root is an invalid StorageReference
StorageReference null_ref = space_ref.Root().Parent();

Metodi di riferimento

Puoi esaminare i riferimenti per comprendere meglio i file a cui puntano utilizzando i metodi full_path , name e bucket . Questi metodi ottengono il percorso completo, il nome e il bucket del file.

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
space_ref.full_path();

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
space_ref.name();

// Reference's bucket is the name of the Cloud Storage bucket where files are stored
space_ref.bucket();

Limitazioni sui riferimenti

I percorsi e i nomi di riferimento possono contenere qualsiasi sequenza di caratteri Unicode validi, ma vengono imposte alcune restrizioni, tra cui:

  1. La lunghezza totale di reference.fullPath deve essere compresa tra 1 e 1024 byte quando viene codificato UTF-8.
  2. Nessun carattere di ritorno a capo o avanzamento riga.
  3. Evitare di utilizzare # , [ , ] , * o ? , poiché non funzionano bene con altri strumenti come Firebase Realtime Database o gsutil .

Esempio completo

Storage* storage = Storage::GetInstance(app);

// Points to the root reference
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
std::string filename = "space.jpg";
StorageReference space_ref = images_ref.Child(filename);

// File path is "images/space.jpg"
std::string path = space_ref.full_path()

// File name is "space.jpg"
std::string name = space_ref.name()

// Points to "images"
StorageReference images_ref = space_ref.Parent();

Prossimi passi

Successivamente, impariamo come caricare file su Cloud Storage.