I've been playing around with Firebase Cloud Storage lately in Swift, and I've come across a scenario where I would like to retrieve images (and only images) in a "folder" (that has other sub folders in it) stored in the bucket. In other words, I want to use recursion to access the files nested in many folders.
I've seen examples done such as this, but of course, this is by using Swift's FileManager
, so I won't be able to utilize the same feature on Firebase Cloud Storage.
Also, keep in mind that I don't want to have a hard-coded reference path such as storage.reference().child("Hard-coded path")
This is what I have so far without using recursion:
class FIRQueries {
var storage: Storage
var storageRef: StorageReference
init() {
storage = Storage.storage()
storageRef = storage.reference()
}
func listFilesInCloud(completion: @escaping(Result<StorageListResult, Error>) -> ()) {
self.storageRef.root().listAll { result, error in
if let error = error {
completion(.failure(error))
print(error.localizedDescription)
return
} else {
completion(.success(result))
for prefix in result.prefixes {
completion(.success(prefix.fullPath))
}
for item in result.items {
print(item)
}
}
}
}
and receiving the results in a SwiftUI .onAppear
block
.onAppear {
FIRQueries().listFilesInCloud { value in
switch value {
case .failure(let error):
print(error)
case .success(let result):
for prefix in result.prefixes {
print(prefix.fullPath)
}
}
}
}
Soo... is there any way to achieve this? With this code, I've only managed to get the names of the "folder" under the root of the bucket. I know I can set a child
after self.storageRef.root()
and give it a path, but then it'll be a hard-coded path.
Any help is appreciated! Thank you!