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
Per caricare o scaricare file, eliminare file o ottenere o aggiornare metadati, è necessario creare un riferimento al file su cui si desidera operare. Un riferimento può essere considerato come un puntatore a un file nel cloud. I riferimenti sono leggeri, quindi puoi crearne quanti ne hai bisogno e sono anche riutilizzabili per più operazioni.
Per creare un riferimento, ottieni un'istanza del servizio di archiviazione utilizzando getStorage()
, quindi chiama ref()
con il servizio come argomento. Questo riferimento punta alla radice del tuo bucket Cloud Storage.
Web modular API
import { getStorage, ref } from "firebase/storage"; // Get a reference to the storage service, which is used to create references in your storage bucket const storage = getStorage(); // Create a storage reference from our storage service const storageRef = ref(storage);
Web namespaced API
// Get a reference to the storage service, which is used to create references in your storage bucket var storage = firebase.storage(); // Create a storage reference from our storage service var storageRef = storage.ref();
Puoi creare un riferimento a una posizione più in basso nell'albero, diciamo 'images/space.jpg'
passando questo percorso come secondo argomento quando chiami ref()
.
Web modular API
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); // Create a child reference const imagesRef = ref(storage, 'images'); // imagesRef now points to 'images' // Child references can also take paths delimited by '/' const spaceRef = ref(storage, 'images/space.jpg'); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images"
Web namespaced API
// Create a child reference var imagesRef = storageRef.child('images'); // imagesRef now points to 'images' // Child references can also take paths delimited by '/' var spaceRef = storageRef.child('images/space.jpg'); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images"
Naviga con Riferimenti
Puoi anche utilizzare le proprietà parent
e root
per spostarti nella gerarchia dei file. parent
sale di un livello, mentre root
naviga fino in cima.
Web modular API
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // Parent allows us to move to the parent of a reference const imagesRef = spaceRef.parent; // imagesRef now points to 'images' // Root allows us to move all the way back to the top of our bucket const rootRef = spaceRef.root; // rootRef now points to the root
Web namespaced API
// Parent allows us to move to the parent of a reference var imagesRef = spaceRef.parent; // imagesRef now points to 'images' // Root allows us to move all the way back to the top of our bucket var rootRef = spaceRef.root; // rootRef now points to the root
child()
, parent
e root
possono essere concatenati insieme più volte, poiché ognuno restituisce un riferimento. L'eccezione è il parent
di root
, che è null
.
Web modular API
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // References can be chained together multiple times const earthRef = ref(spaceRef.parent, 'earth.jpg'); // earthRef points to 'images/earth.jpg' // nullRef is null, since the parent of root is null const nullRef = spaceRef.root.parent;
Web namespaced API
// References can be chained together multiple times var earthRef = spaceRef.parent.child('earth.jpg'); // earthRef points to 'images/earth.jpg' // nullRef is null, since the parent of root is null var nullRef = spaceRef.root.parent;
Proprietà di riferimento
Puoi ispezionare i riferimenti per comprendere meglio i file a cui puntano utilizzando le proprietà fullPath
, name
e bucket
. Queste proprietà ottengono il percorso completo del file, il nome del file e il bucket in cui è archiviato il file.
Web modular API
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // Reference's path is: 'images/space.jpg' // This is analogous to a file path on disk spaceRef.fullPath; // 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 where files are stored spaceRef.bucket;
Web namespaced API
// Reference's path is: 'images/space.jpg' // This is analogous to a file path on disk spaceRef.fullPath; // 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 where files are stored spaceRef.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:
- 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
Web modular API
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); // Points to the root reference const storageRef = ref(storage); // Points to 'images' const imagesRef = ref(storageRef, 'images'); // Points to 'images/space.jpg' // Note that you can use variables to create child values const fileName = 'space.jpg'; const spaceRef = ref(imagesRef, fileName); // File path is 'images/space.jpg' const path = spaceRef.fullPath; // File name is 'space.jpg' const name = spaceRef.name; // Points to 'images' const imagesRefAgain = spaceRef.parent;
Web namespaced API
// Points to the root reference var storageRef = firebase.storage().ref(); // Points to 'images' var imagesRef = storageRef.child('images'); // Points to 'images/space.jpg' // Note that you can use variables to create child values var fileName = 'space.jpg'; var spaceRef = imagesRef.child(fileName); // File path is 'images/space.jpg' var path = spaceRef.fullPath; // File name is 'space.jpg' var name = spaceRef.name; // Points to 'images' var imagesRef = spaceRef.parent;
Successivamente, impariamo come caricare i file su Cloud Storage.