2016-06-10 20 views
7

FirebaseStorage zawsze zwraca błąd 400 podczas próby usunięcia katalogu, np. Coś takiego jak poniżej zawsze zwraca błąd 400.FirebaseStorage: Jak usunąć katalog

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

Jednak usunięcie pliku działa dobrze, np. coś takiego nie zwróci błędu:

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

Co mogę tu robić źle? Nie sądzę, że nie jest obsługiwany przez FirebaseStorage, ponieważ usuwanie plików z katalogu jeden po drugim byłoby całkiem kiepskie (szczególnie jeśli wspomniany katalog ma 100 lub 1000).

Odpowiedz

11

z grupy google, usunięcie katalogu nie jest możliwe. Musisz gdzieś przechowywać listę plików (w bazie danych Firebase) i usuwać je jeden po drugim.

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

Mam również złożyła wniosek fabularny, ale ponieważ ich bug tracker nie jest publiczna, nie ma żadnego związku, że mogę dzielić.

+1

Dlaczego google. Tylko ... Tylko dlaczego? – Ruan

0

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

Użyj tego kodu.

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

Jak wskazano powyżej, usunięcie katalogu jest nieprawidłowe. Dzielę się przykładem odpytywania listy plików w bazie danych Firebase i usuwania ich pojedynczo. To jest moje zapytanie i połączenie.

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

To jest moja funkcja polegająca na wyszukiwaniu adresu URL, a następnie usuwaniu pliku z tego odniesienia.

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)") 
        } 
       }) 
      } 
     } 
    }) 
}