I tuoi file vengono archiviati in un Cloud Storage bucket. I file in questo bucket vengono 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 ottiene l'accesso. Questi riferimenti possono essere utilizzati per caricare o scaricare dati, recuperare o aggiornare i 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 il Firebase Realtime Database, questi percorsi ti sembreranno molto familiari. Tuttavia, i dati dei file vengono archiviati in Cloud Storage, non nel Realtime Database.
Creazione di un riferimento
Per caricare o scaricare file, eliminare file o recuperare o aggiornare i metadati, devi creare un riferimento al file su cui vuoi operare. Un riferimento può essere considerato un puntatore a un file nel cloud. I riferimenti sono leggeri, quindi puoi crearne quanti ne hai bisogno e sono riutilizzabili per più operazioni.
Per creare un riferimento, recupera un'istanza del servizio Storage utilizzando getStorage() e poi chiama ref() con il servizio come argomento.
Questo riferimento punta alla radice del tuo Cloud Storage bucket.
Web
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
// 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,
ad esempio 'images/space.jpg' passando questo percorso come secondo argomento quando
chiami ref().
Web
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
// 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"
Navigazione con i riferimenti
Puoi anche utilizzare le proprietà parent e root per navigare nella gerarchia dei file. parent consente di spostarsi di un livello, mentre root consente di spostarsi fino alla parte superiore.
Web
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
// 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 più volte, poiché ognuno restituisce un riferimento. L'eccezione è il parent di root, che è null.
Web
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
// 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 esaminare i riferimenti per comprendere meglio i file a cui puntano utilizzando le proprietà fullPath, name e bucket. Queste proprietà recuperano il percorso completo del file, il nome del file e il bucket in cui è archiviato il file.
Web
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
// 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 dei riferimenti
I percorsi e i nomi dei riferimenti possono contenere qualsiasi sequenza di caratteri Unicode validi, ma sono soggette a determinate restrizioni, tra cui:
- La lunghezza totale di
reference.fullPathdeve essere compresa tra 1 e 1024 byte se codificata in UTF-8. - Nessun carattere di ritorno a capo o avanzamento riga.
- Evita di utilizzare
#,[,],*, o?, in quanto non funzionano correttamente con altri strumenti come Firebase Realtime Database o gsutil.
Esempio completo
Web
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
// 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;
Scopriamo ora come caricare i file in Cloud Storage.