【问题标题】:How to Use Recursion to get all Files in a Directory Tree for Firebase Cloud Storage如何使用递归获取 Firebase 云存储目录树中的所有文件
【发布时间】:2021-10-18 04:33:12
【问题描述】:

我最近在 Swift 中使用 Firebase Cloud Storage,我遇到了一个场景,我想在“文件夹”(其中包含其他子文件夹)中检索图像(并且只有图像) ) 存储在桶中。换句话说,我想使用递归来访问嵌套在许多文件夹中的文件。

我已经看到了诸如 this 之类的示例,但当然,这是使用 Swift 的 FileManager,所以我无法在 Firebase Cloud Storage 上使用相同的功能。

另外,请记住,我不希望有硬编码的引用路径,例如 storage.reference().child("Hard-coded path")

这是我到目前为止没有使用递归的:

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

并在 SwiftUI .onAppear 块中接收结果

.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...有什么办法可以做到这一点?使用此代码,我只能设法获取存储桶根目录下的“文件夹”的名称。我知道我可以在self.storageRef.root() 之后设置一个child 并给它一个路径,但它会是一个硬编码的路径。

感谢任何帮助!谢谢!

【问题讨论】:

    标签: swift google-cloud-storage firebase-storage


    【解决方案1】:

    正如 listing all files 上的 Firebase 文档所示,您可以在每个前缀上递归调用 listAll(completion:)

    所以:

    for prefix in result.prefixes {
        print(prefix.fullPath)
        prefix.listAll(...) // ? List files in subdirectory
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-09
      • 2017-05-18
      • 2020-01-31
      • 2011-04-26
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2020-09-11
      • 2018-07-02
      相关资源
      最近更新 更多