Ashfaqur_Rahman
Ashfaqur_Rahman

Reputation: 158

Unable to upload image into Firebase Storage - swift 5

I can't upload an image into Firebase Storage I found this error in my output log

"An SSL error has occurred and a secure connection to the server cannot be made."

and last output is

print("check: 4 >> Don't put image")

Then the least of the code doesn't execute therefore the image still not uploading

In the putData method i also use url parameter but also got the same problem

MY Full code is

import UIKit
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage

class ViewController: UIViewController {

    @IBOutlet weak var imageview: UIImageView!
    @IBOutlet weak var emailTextView: UITextField!
    @IBOutlet weak var passwordTextView: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func buttonAction(_ sender: Any) {

        Auth.auth().createUser(withEmail: emailTextView.text!, password: passwordTextView.text!){ (result, error) in
            if let _eror = error {
                print(_eror.localizedDescription)
                return
            }
            print("check: 1 >> After creating user\(result!)")


            let uid = result?.user.uid
            let storage = Storage.storage()
            let storageRef = storage.reference(forURL: "gs://porate-chai-4deee.appspot.com").child("profile_image").child(uid!)
            print("check: 2 >> \(storageRef)")


            if let profileImage = self.imageview!.image, let imageData = profileImage.jpegData(compressionQuality: 0.1) {
                print("check: 3 >> \(imageData)")
                storageRef.putData(imageData, metadata: nil) { (metadata, error) in
                    if error != nil {
                        print("check: 4 >> Don't put image")
                        return
                    }
                    print("put image on firebase storage")
                    storageRef.downloadURL(completion: {(url, error) in
                        if error != nil {
                            print(error!.localizedDescription)
                            return
                        }
                        let downloadURL = url?.absoluteString
                        print(downloadURL!)
                        let ref = Database.database().reference()
                        print("seeref\(ref)")
                        let userReference = ref.child("tutor")
                        let newUserReference = userReference.child(uid!)
                        newUserReference.setValue([
                            "email": self.emailTextView.text!,
                            "profileimageurl": downloadURL!
                        ])
                    })
                }
            }
        }
    }
}

My Firebase Storage Rules is

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

I also try this rules

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

enter image description here

Upvotes: 0

Views: 456

Answers (1)

Ashfaqur_Rahman
Ashfaqur_Rahman

Reputation: 158

With the help of this answer "flutter pub get" can't get dependency plugins on Windows

After 27-03-2020 the code still work but after 28-03-2020 have some problem storage.googleapis.com in Bangladesh.

To overcome this issue, use VPN tool and re-run your project. I used Hotspot Sheild VPN and after that everything was good.

I use this Hotspot Shield: Fastest VPNwww.hotspotshield.com

Then the file again uplod

Upvotes: 1

Related Questions