2016-06-10 114 views
7

FirebaseStorage總是返回錯誤400當我試圖刪除一個目錄,即像下面總是返回錯誤400FirebaseStorage:如何刪除目錄

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

但是,刪除文件可以正常工作,例如,類似的東西不會返回錯誤:

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

我在這裏做錯了什麼?我不估計它不是由FirebaseStorage支持,因爲一個從目錄中刪除一個文件將是非常跛腳(特別是如果上述目錄中有數百或這些1000)。

回答

11

從谷歌組,刪除一個目錄是不可能的。您必須在某處(在Firebase數據庫中)維護一個文件列表並逐個刪除它們。

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

我還提交了功能要求,但由於他們的bug跟蹤系統是不公開的,有沒有聯繫,我可以分享。

+1

爲什麼谷歌。只是...爲什麼? – Ruan

0

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

使用此代碼。

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

如上所述,刪除一個目錄是無效的。我分享查詢在火力地堡數據庫文件的列表,並逐個刪除它們中的一個例子。這是我的查詢和電話。

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

這是我的函數循環獲取URL,然後刪除該存儲引用中的文件。

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