Firebase Storage Download Response Error

1k views Asked by At

I have been able to successfully upload images to firebase storage but have been unable to successfully download the image.

I have attempted to download images in all three of the suggested ways on Firebase Storage Guides:

1) Download to NSData in memory 2) Download to an NSURL representing a file on device 3) Generate an NSURL representing the file online

An example is below of two different attempts:

func loadProfileImage() {
        guard let currentUser = Auth.auth().currentUser else { return }
        let profilePhotoFile = "ProfileImages/" + currentUser.uid
        let reference = Storage.storage().reference(withPath: profilePhotoFile)

#1st Attempt downloading to memory:

    reference.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
        if let error = error {
            print("an error occurred: \(error)")
            print("see data response: \(data)")
        }  else {
            self.profileView.image = UIImage(data: data!)
        }
     }

#2nd Attempt with download url:

     reference.downloadURL { (url, error) in
        if let error = error {
            print(error)
        } else {
            self.profileView.sd_setImage(with: url, placeholderImage: 
           #imageLiteral(resourceName: "placeHolderProfileView")) { 
         (image, error, type, reference2) in
                print("reference location of image in the google 
     bucket: \(reference2)")
                print("error retrieving image: \(String(describing: 
            error))")
                print("type: \(type)")
                print("image details: \(String(describing: image))")
            }

        }

    }
}

Also tried using alamofire instead of SDWebImage to see if error code was same and it is the same see below:

Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response." UserInfo={object=ProfileImages/6I2RhzFI3edYNph9J4WsaXXXX, ResponseErrorCode=100, bucket=bXXXX-production.appspot.com, NSLocalizedDescription=An unknown error occurred, please check the server response., ResponseErrorDomain=NSPOSIXErrorDomain, _kCFStreamErrorDomainKey=1, NSErrorPeerAddressKey={length = 28, capacity = 28, bytes = 0x1c1e01bb000000002607f8b040090813 ... 0000200a00000000}, _kCFStreamErrorCodeKey=100}

I have checked and rechecked the google storage bucket location and believe I have the reference location correct (using the same as the upload file path which works correctly).

Any help would be much appreciated

2

There are 2 answers

0
Sam Jensen On BEST ANSWER

Realized the error was in headers that were included when uploading the image:

I had originally listed the following with the upload, by commenting them out I was able to successfully download with SDWebImage and the suggestion from vbuzze.

let uploadMetadata = StorageMetadata() uploadMetadata.contentType = "image/jpeg" uploadMetadata.customMetadata = ["Profile Name" : currentUser.displayName] as? [String : String]

3
vbuzze On

There you go :

func downloadImage(url : String,
                       completionHandler: @escaping (Bool?, UIImage?, String?) -> Void) -> Void
    {
        var success : Bool = false
        var img : UIImage? = nil
        var errorLog : String? = nil

        let u = URL(string: url)

        let task = URLSession.shared.dataTask(with: u!, completionHandler: { (data, response, error) in

            if error != nil
            {
                errorLog = error?.localizedDescription
                completionHandler(success, img, errorLog)
            }
            else
            {
                success = true
                img = UIImage(data: data!)
                completionHandler(usuccess, img, errorLog)
            }

        })

        task.resume()

    }

Get URL using :

imgReference.downloadURL { (url, error) in
   guard let url = url else { return } 
   urlString = url.absoluteString
   //do something with the urlString (such as download image)
}