Crea un riferimento Cloud Storage su Android

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.

Crea un riferimento utilizzando l'istanza singleton FirebaseStorage e chiamando il relativo metodo getReference() .

Kotlin+KTX

// Create a storage reference from our app
var storageRef = storage.reference

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

Successivamente, 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.

Kotlin+KTX

// Create a child reference
// imagesRef now points to "images"
var imagesRef: StorageReference? = storageRef.child("images")

// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
var spaceRef = storageRef.child("images/space.jpg")

Java

// Create a child reference
// imagesRef now points to "images"
StorageReference imagesRef = storageRef.child("images");

// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
StorageReference spaceRef = storageRef.child("images/space.jpg");

Puoi anche utilizzare i metodi getParent() e getRoot() per navigare nella nostra gerarchia di file. getParent() naviga verso l'alto di un livello, mentre getRoot() naviga fino in cima.

Kotlin+KTX

// parent allows us to move our reference to a parent node
// imagesRef now points to 'images'
imagesRef = spaceRef.parent

// root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
val rootRef = spaceRef.root

Java

// getParent allows us to move our reference to a parent node
// imagesRef now points to 'images'
imagesRef = spaceRef.getParent();

// getRoot allows us to move all the way back to the top of our bucket
// rootRef now points to the root
StorageReference rootRef = spaceRef.getRoot();

child() , getParent() e getRoot() possono essere concatenati più volte, poiché ciascuno restituisce un riferimento. Ma chiamare getRoot().getParent() restituisce null .

Kotlin+KTX

// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
val earthRef = spaceRef.parent?.child("earth.jpg")

// nullRef is null, since the parent of root is null
val nullRef = spaceRef.root.parent

Java

// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
StorageReference earthRef = spaceRef.getParent().child("earth.jpg");

// nullRef is null, since the parent of root is null
StorageReference nullRef = spaceRef.getRoot().getParent();

Proprietà di riferimento

Puoi esaminare i riferimenti per comprendere meglio i file a cui puntano utilizzando i metodi getPath() , getName() e getBucket() . Questi metodi ottengono il percorso completo, il nome e il bucket del file.

Kotlin+KTX

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.path

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

// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.bucket

Java

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

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

// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.getBucket();

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

Kotlin+KTX

// Points to the root reference
storageRef = storage.reference

// Points to "images"
imagesRef = storageRef.child("images")

// Points to "images/space.jpg"
// Note that you can use variables to create child values
val fileName = "space.jpg"
spaceRef = imagesRef.child(fileName)

// File path is "images/space.jpg"
val path = spaceRef.path

// File name is "space.jpg"
val name = spaceRef.name

// Points to "images"
imagesRef = spaceRef.parent

Java

// Points to the root reference
storageRef = storage.getReference();

// Points to "images"
imagesRef = storageRef.child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
String fileName = "space.jpg";
spaceRef = imagesRef.child(fileName);

// File path is "images/space.jpg"
String path = spaceRef.getPath();

// File name is "space.jpg"
String name = spaceRef.getName();

// Points to "images"
imagesRef = spaceRef.getParent();

Successivamente, impariamo come caricare file su Cloud Storage.