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.
I riferimenti vengono creati utilizzando il servizio FirebaseStorage
e chiamando il relativo metodo reference
.
Rapido
// Get a reference to the storage service using the default Firebase App let storage = Storage.storage() // Create a storage reference from our storage service let storageRef = storage.reference()
Obiettivo-C
// Get a reference to the storage service using the default Firebase App FIRStorage *storage = [FIRStorage storage]; // Create a storage reference from our storage service FIRStorageReference *storageRef = [storage reference];
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.
Rapido
// Create a child reference // imagesRef now points to "images" let imagesRef = storageRef.child("images") // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" var spaceRef = storageRef.child("images/space.jpg") // This is equivalent to creating the full reference let storagePath = "\(your_firebase_storage_bucket)/images/space.jpg" spaceRef = storage.reference(forURL: storagePath)
Obiettivo-C
// Create a child reference // imagesRef now points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" FIRStorageReference *spaceRef = [storageRef child:@"images/space.jpg"]; // This is equivalent to creating the full reference spaceRef = [storage referenceForURL:@"gs://<your-firebase-storage-bucket>/images/space.jpg"];
Naviga con Riferimenti
Puoi anche utilizzare i metodi parent
e root
per navigare nella nostra gerarchia di file. parent
sale di un livello, mentre root
naviga fino in cima.
Rapido
// Parent allows us to move to the parent of a reference // imagesRef now points to 'images' let imagesRef = spaceRef.parent() // Root allows us to move all the way back to the top of our bucket // rootRef now points to the root let rootRef = spaceRef.root()
Obiettivo-C
// Parent allows us to move to the parent of a reference // 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 FIRStorageReference *rootRef = [spaceRef root];
child
, parent
e root
possono essere concatenati insieme più volte, poiché ognuno restituisce un riferimento. L'eccezione è il parent
di root
, che è nil
.
Rapido
// References can be chained together multiple times // earthRef points to "images/earth.jpg" let earthRef = spaceRef.parent()?.child("earth.jpg") // nilRef is nil, since the parent of root is nil let nilRef = spaceRef.root().parent()
Obiettivo-C
// References can be chained together multiple times // earthRef points to "images/earth.jpg" FIRStorageReference *earthRef = [[spaceRef parent] child:@"earth.jpg"]; // nilRef is nil, since the parent of root is nil FIRStorageReference *nilRef = [[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, il nome e il bucket del file.
Rapido
// 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
Obiettivo-C
// 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
Rapido
// Points to the root reference let storageRef = Storage.storage().reference() // Points to "images" let imagesRef = storageRef.child("images") // Points to "images/space.jpg" // Note that you can use variables to create child values let fileName = "space.jpg" let spaceRef = imagesRef.child(fileName) // File path is "images/space.jpg" let path = spaceRef.fullPath // File name is "space.jpg" let name = spaceRef.name // Points to "images" let images = spaceRef.parent()
Obiettivo-C
// Points to the root reference FIRStorageReference *storageRef = [[FIRStorage storage] reference]; // Points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Points to "images/space.jpg" // Note that you can use variables to create child values NSString *fileName = @"space.jpg"; FIRStorageReference *spaceRef = [imagesRef child:fileName]; // File path is "images/space.jpg" NSString *path = spaceRef.fullPath; // File name is "space.jpg" NSString *name = spaceRef.name; // Points to "images" imagesRef = [spaceRef parent];
Successivamente, impariamo come caricare i file su Cloud Storage.