2016-06-10 33 views
7

FirebaseStorage restituisce sempre l'errore 400 quando si tenta di eliminare una directory, ad esempio qualcosa del seguente restituisce sempre l'errore 400.FirebaseStorage: Come eliminare la directory

let storageRef = FIRStorage.storage().reference().child("path/to/directory") 
storageRef.deleteWithCompletion { (error) in 
    print("error: \(error)") // always prints error code 400 
} 

Tuttavia, l'eliminazione di un file funziona correttamente ad es. qualcosa di simile non restituisce un errore:

let storageRef = FIRStorage.storage().reference().child("path/to/file.jpg") 
storageRef.deleteWithCompletion { (error) in 
    print("error: \(error)") // works fine, error is nil 
} 

Cosa potrei fare di sbagliato qui? Non ritengo che non sia supportato da FirebaseStorage perché l'eliminazione di file da una directory uno per uno sarebbe piuttosto loppa (specialmente se la suddetta directory ne ha 100 o 1000).

risposta

11

da google group, l'eliminazione di una directory non è possibile. Devi mantenere un elenco di file da qualche parte (nel database Firebase) ed eliminarli uno per uno.

https://groups.google.com/forum/#!topic/firebase-talk/aG7GSR7kVtw

ho anche presentato la richiesta di funzionalità, ma dato che la loro bug tracker non è pubblico, non c'è alcun legame che ho potuto condividere.

+1

Perché google. Solo ... Solo perché? – Ruan

0

In 26/5/2017 there is no way to delete directory But you can use my algorithm

Utilizzare questo codice.

this.sliders = this.db.list(`users/${this.USER_UID}/website/sliders`) as FirebaseListObservable<Slider[]> 



    /** 
    * Delete image from firebase storage is take a string path of the image 
    * @param _image_path 
    */ 
    deleteImage(_image_path: string) { 

    // first delete the image 
    const storageRef = firebase.storage().ref(); 
    const imageRef = storageRef.child(_image_path); 
    imageRef.delete().then(function() { 
     console.log('file deleted'); 
     // File deleted successfully 
    }).catch(function(error) { 
     // Uh-oh, an error occurred! 
     console.log(error); 
    }); 

    } 



    /** 
    * Deletes multiple Sliders, it takes an array of ids 
    * @param ids 
    */ 
    deleteMutipleSliders(ids: any) { 

    ids.forEach(id => { 

     this.getSliderDetails(id).subscribe(slider => { 

     let id = slider.$key; // i think this is not nesesery 
     const imgPath = slider.path; 

     this.deleteImage(imgPath); 
     }); 

     return this.sliders.remove(id); 

    }); 


    } 
0

Come indicato sopra, l'eliminazione di una directory non è valida. Sto condividendo un esempio di interrogare un elenco di file nel database Firebase e cancellarli uno per uno. Questa è la mia richiesta e chiamata.

let messagePhotoQuery = messagesRef.child(group.key).child("messages").queryOrdered(byChild: "photoURL") 
    deleteMessagePhotos(from: messagePhotoQuery) 

Questa è la mia funzione che esegue il ciclo di ricerca dell'URL, quindi elimina il file nel riferimento di archiviazione.

func deleteMessagePhotos(from photoQuery: FIRDatabaseQuery) { 
    photoQuery.observeSingleEvent(of: .value, with: { (messagesSnapshot) in 
     guard messagesSnapshot.exists() else { return } 
     print(messagesSnapshot) 
     for message in messagesSnapshot.children { 
      let messageSnapshot = message as! FIRDataSnapshot 
      let messageData = messageSnapshot.value as! [String: AnyObject] 
      if let photoURL = messageData["photoURL"] as? String { 
       let photoStorageRef = FIRStorage.storage().reference(forURL: photoURL) 
       photoStorageRef.delete(completion: { (error) in 
        if let error = error { 
         print(error) 
        } else { 
         // success 
         print("deleted \(photoURL)") 
        } 
       }) 
      } 
     } 
    }) 
} 
Problemi correlati