I tuoi file sono 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 database in tempo reale di Firebase. Creando un riferimento a un file, la tua app ottiene l'accesso ad esso. 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 oa un nodo di livello superiore nella gerarchia.
Se hai utilizzato Firebase Realtime Database , questi percorsi dovrebbero sembrarti molto familiari. Tuttavia, i dati dei file vengono archiviati in Cloud Storage, non nel database in tempo reale.
Crea un riferimento
Crea un riferimento per caricare, scaricare o eliminare un file o 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");
Naviga con Riferimenti
Puoi anche usare i metodi getParent()
e getRoot()
per navigare nella nostra gerarchia di file. getParent()
sale di un livello, mentre getRoot()
sale 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 insieme più volte, poiché ognuno restituisce un riferimento. Ma chiamando 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 ispezionare 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:
- La lunghezza totale di reference.fullPath deve essere compresa tra 1 e 1024 byte con codifica UTF-8.
- Nessun carattere di ritorno a capo o avanzamento riga.
- Evita di usare
#
,[
,]
,*
o?
, poiché questi 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 i file su Cloud Storage.