Crea un riferimento Cloud Storage su piattaforme Apple

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.

I riferimenti vengono creati utilizzando il servizio FirebaseStorage e chiamando il relativo metodo reference .

Veloce

// 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.

Veloce

// 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"];
     

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.

Veloce

// 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é ciascuno restituisce un riferimento. L'eccezione è il parent di root , che è nil .

Veloce

// 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 esaminare 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.

Veloce

// 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:

  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

Veloce

// 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 file su Cloud Storage.