Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of type 'StorageMetadata' has no member 'downloadURL'

I just updated Firebase Storage to 5.0.0 and it looks like metadata.downloadURL() is not recognized anymore. (Value of type 'StorageMetadata' has no member 'downloadURL')

Though after looking in the documentation it should still be available :

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

The project was cleaned & rebuilt already.

Am I missing something ?

like image 923
vbuzze Avatar asked May 10 '18 20:05

vbuzze


3 Answers

Can you try

// 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'
  }
}
like image 100
Sh_Khan Avatar answered Nov 01 '22 19:11

Sh_Khan


This is my version for Swift 3 / Swift 4.

Explanation of what happens in the code.

This is essentially the same answer as Sh_Khan's. But in his example the User already knows the bucket path. In my example, we get the path from an upload task. This was what has lead me to this question as well as what I think op was looking for as he was looking for metadata.downloadURL() replacement.

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

}
like image 25
David Seek Avatar answered Nov 01 '22 18:11

David Seek


Let's try this code in Swift 4.2:

let imgData = UIImage.jpegData(self.imageView.image!)

let imageName = UUID().uuidString
let ref = Storage.storage().reference().child("pictures/\(imageName).jpg")
let meta = StorageMetadata()
meta.contentType = "image/jpeg"

self.uploadToCloud(data: imgData(0.5)!, ref: ref, meta: meta)

UploadToCloud Method:

` Method UploadToCloud
func uploadToCloud(data:Data, ref:StorageReference, meta:StorageMetadata) {
    ref.putData(data, metadata: meta) { (metaData, error) in
        if let e = error {
            print("==> error: \(e.localizedDescription)")
        }
        else 
        {
            ref.downloadURL(completion: { (url, error) in
                print("Image URL: \((url?.absoluteString)!)")
            })
        }
    }
}
like image 2
Saravit Soeng Avatar answered Nov 01 '22 20:11

Saravit Soeng