簡體   English   中英

無法將圖像上傳到 Firebase 存儲 - swift 5

[英]Unable to upload image into Firebase Storage - swift 5

我無法將圖像上傳到 Firebase Storage 我在輸出日志中發現了這個錯誤

“發生 SSL 錯誤,無法與服務器建立安全連接。”

最后輸出是

打印(“檢查: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;
    }
  }
}

在此處輸入圖片說明

借助此答案“flutter pub get”無法在 Windows 上獲取依賴項插件

在 27-03-2020 之后代碼仍然有效,但在 28-03-2020 之后在孟加拉國的 storage.googleapis.com 出現了一些問題。

要解決此問題,請使用 VPN 工具並重新運行您的項目。 我使用了 Hotspot Sheild VPN,之后一切都很好。

我使用這個 Hotspot Shield: Fastest VPNwww.hotspotshield.com

然后文件再次上傳

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM