metadata.downloadURL()不再被识别?

vbu*_*zze 5 xcode firebase swift firebase-storage swift4

我刚刚将Firebase存储更新到5.0.0,它看起来metadata.downloadURL()已经不再被识别了.(Value of type 'StorageMetadata' has no member 'downloadURL')

虽然在查看文档后仍应该可用:

https://firebase.google.com/docs/reference/swift/firebasestorage/api/reference/Classes/StorageMetadata#/c:objc(cs)FIRStorageMetadata(im)downloadURL

该项目已经清理和重建.

我错过了什么吗?

Sh_*_*han 15

你能试一下吗

// Create a reference to the file you want to download
let starsRef = storageRef.child("images/stars.jpg")

// Fetch the download URL
starsRef.downloadURL { url, error in
  if let error = error {
    // Handle any errors
  } else {
    // Get the download URL for 'images/stars.jpg'
  }
}
Run Code Online (Sandbox Code Playgroud)

  • *firebaser here*下载URL曾经包含在任务的元数据中,但这对性能有影响,而并非所有用户都需要它.这就是为什么它现在总是需要单独的调用,以便只有需要下载URL的用户才能看到性能影响. (2认同)

Dav*_*eek 6

这是我的 Swift 3 / Swift 4 版本。

解释代码中发生的事情。

这基本上与 Sh_Khan 的答案相同。但是在他的示例中,用户已经知道存储桶路径。在我的示例中,我们从上传任务获取路径。这就是导致我提出这个问题的原因,也是我认为 op 在寻找metadata.downloadURL()替代品时正在寻找的东西。

class StorageManagager {


    private let storageReference: StorageReference

    init() {

        // first we create a reference to our storage
        // replace the URL with your firebase URL
        self.storageReference = Storage.storage().reference(forURL: "gs://MYAPP.appspot.com")
    }

    // MARK: - UPLOAD DATA
    open func uploadData(_ data: Data, named filename: String, completion: @escaping (URL? , Error?) -> Void) {

        let reference = self.storageReference.child(filename)
        let metadata = StorageMetadata()
        metadata.contentType = "ourType" // in my example this was "PDF"

        // we create an upload task using our reference and upload the 
        // data using the metadata object
        let uploadTask = reference.putData(data, metadata: metadata) { metadata, error in

            // first we check if the error is nil
            if let error = error {

                completion(nil, error)
                return
            }

            // then we check if the metadata and path exists
            // if the error was nil, we expect the metadata and path to exist
            // therefore if not, we return an error
            guard let metadata = metadata, let path = metadata.path else {
                completion(nil, NSError(domain: "core", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unexpected error. Path is nil."]))
                return
            }

            // now we get the download url using the path
            // and the basic reference object (without child paths)
            self.getDownloadURL(from: path, completion: completion)
        }

        // further we are able to use the uploadTask for example to 
        // to get the progress
    }

    // MARK: - GET DOWNLOAD URL
    private func getDownloadURL(from path: String, completion: @escaping (URL?, Error?) -> Void) {

        self.storageReference.child(path).downloadURL(completion: completion)
    }

}
Run Code Online (Sandbox Code Playgroud)