【问题标题】:Unable to upload image into Firebase Storage - swift 5无法将图像上传到 Firebase 存储 - swift 5
【发布时间】:2020-07-10 05:52:05
【问题描述】:

我无法将图片上传到 Firebase 存储 我在输出日志中发现了这个错误

“发生 SSL 错误并且与服务器的安全连接 无法制作。”

最后的输出是

print("check: 4 >> 不要放图片")

那么最少的代码没有执行因此图像仍然没有上传

putData 方法中,我也使用了 url 参数,但也遇到了同样的问题

我的完整代码是

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

我的 Firebase 存储规则是

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

我也试试这个规则

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

【问题讨论】:

标签: ios firebase firebase-storage swift5


【解决方案1】:

在这个答案的帮助下 "flutter pub get" can't get dependency plugins on Windows

2020 年 3 月 27 日之后,代码仍然有效,但 2020 年 3 月 28 日之后,孟加拉国的 storage.googleapis.com 出现了一些问题。

要解决此问题,请使用 VPN 工具并重新运行您的项目。 我使用了 Hotspot Sheild VPN,之后一切都很好。

我用这个 热点盾:最快 VPNwww.hotspotshield.com

然后文件再次上传

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 2019-01-30
    • 2021-07-28
    • 2018-12-01
    • 2018-01-13
    • 1970-01-01
    • 2020-03-05
    • 2017-09-25
    相关资源
    最近更新 更多