【问题标题】:Swift Firebase Storage get all Download URL's of a specific childSwift Firebase Storage 获取特定子项的所有下载 URL
【发布时间】:2021-02-13 08:33:02
【问题描述】:

目前,我可以通过 Firebase 存储参考按 文件名 获取 下载 url

我想在特定子项不使用文件名且仅使用最后一个子项名称检索所有下载 URL

只需将每个下载 url 添加到列表/数组中

我怎样才能用我给定的参考来做到这一点。

func getDownloadURL() {
    
    let ref = Storage.storage().reference()
    
    let fileName = "Lessons_Lesson1_Class1.mp3"
    
    let starsRef = ref.child("Daily Meditations").child("Lessons").child("Lesson 1").child(fileName)
    

    // Fetch the download URL
    starsRef.downloadURL { url, error in
      if let error = error {
        // Handle any errors
        print(error)
      } else {
        // Get the download URL for 'Lessons_Lesson1_Class1.mp3'
        print(url)
      }
    }
   
    
}

Firebase Refrence Docs

【问题讨论】:

  • 似乎您想使用列表文件 API。 firebase.google.com/docs/storage/ios/list-files
  • 所以我使用了列表文件,但结果给了我存储位置而不是 downloadURL。
  • 谢谢@DougStevenson。我通过查看这两个文档找到了一种方法来实现我所需要的。
  • 您必须为找到的每个文件调用一次 getDownloadUrl。

标签: ios swift firebase firebase-storage


【解决方案1】:
let stg = Storage.storage().reference()
let path = "Daily Meditations/Lessons/Lesson 1"

stg.child(path).listAll { (list, error) in
    if let error = error {
        print(error)
    } else {
        let inStorage = list.items.map({ $0.name })
        print(inStorage) // an array of file names in string format
    }
}

我假设路径名中允许使用空格,因为您正在使用它们。要列出路径中的所有文件,请使用listAll。该方法将返回一个StorageListResult 对象,我将其命名为list

https://firebase.google.com/docs/reference/swift/firebasestorage/api/reference/Classes/StorageListResult

【讨论】:

  • 这仅打印文件名。我确定有一种方法可以将文件名操作为 URL,但我已经找到了一个可行的解决方案。谢谢
【解决方案2】:

所以我能够结合 列出所有文件下载 URL 来实现我在 firebase 文档中尝试完成的任务。

代码如下:

func getDownloadURl() {
        
        let ref = Storage.storage().reference()
        
        let storageReference = ref.child("Lessons/Lesson 1")
        storageReference.listAll { (result, error) in
          if let error = error {
            print(error)
          }
        for item in result.items {
            //List storage reference
            let storageLocation = String(describing: item)
            let gsReference = Storage.storage().reference(forURL: storageLocation)
            
            // Fetch the download URL
            gsReference.downloadURL { url, error in
              if let error = error {
                // Handle any errors
                print(error)
              } else {
                // Get the download URL for each item storage location
                print(url!)
              }
            }
            
            
          }
        }
        
    }

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2019-04-17
    • 1970-01-01
    相关资源
    最近更新 更多