nagyf
nagyf

Reputation: 869

IOS Firebase storage file upload - object does not exist

I'm trying to use Firebase on IOS (Swift) to upload an image file chosen by the user to the firebase storage. I've already authenticated the user, so that is not the problem. Below you can see the view controller that I'm trying to use, but when it tries to upload the image, I get back an error message saying:

Object images/vesVLmkqS2cCLQTJOjv9CFe8mh22/0524729A-855E-4E63-8C11-50F4C4B1A905 does not exist.

(you can see in the code that the middle part of this path is the user uid, so I definitely have an authenticated user)

I tried to simplify this path before to test value like "myimage.png" but didn't help, I got the same error.

I've already tried everything I could, please help me because I can't find anything related in the firebase documentation.

The code below automatically opens the image picker when the view loads. If the user chooses an image, we set it to an image view. When the user clicks the upload button, the controller tries to upload the file to firebase storage.

import UIKit
import Firebase

class ShareViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!

    let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .photoLibrary

        present(imagePicker, animated: true, completion: nil)
    }

    @IBAction func onPictureTapped(_ sender: Any) {
        present(imagePicker, animated: true, completion: nil)
    }

    @IBAction func onUploadClicked(_ sender: Any) {
        if let pickedImage = imageView.image, let imageData = UIImagePNGRepresentation(pickedImage) {
            let storageRef = Storage().reference()
            let imageRef = storageRef.child("images/\(Auth.auth().currentUser!.uid)/\(NSUUID().uuidString)")

            imageRef.putData(imageData, metadata: nil) { (metadata, error) in
                if error != nil {
                    NSLog(error!.localizedDescription)
                }

                self.navigationController?.popToRootViewController(animated: true)
            }
        }
    }

    internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imageView.contentMode = .scaleAspectFit
            imageView.image = pickedImage
        }

        self.dismiss(animated: true, completion: nil)
    }
}

Upvotes: 1

Views: 2146

Answers (1)

nagyf
nagyf

Reputation: 869

Ok, I found the problem:

// Instead of this:
Storage().reference()

// You need to use this
Storage.storage().reference()

This is ridiculous that there are no warnings about this, also the app don't crash. Very annoying, and it took me at least 5 hours to find

Upvotes: 8

Related Questions