"an unknown error occurred, please check the server response when uploading video" 从 uiimagepickercontroller 到 firebasestorage (swift)

"an unknown error occurred, please check the server response when uploading video" from uiimagepickercontroller to firebasestorage (swift)

我正在尝试将用户选择的视频(通过 uiimagepickercontroller)上传到 Firebase 存储,但我在 localizedDescription

中遇到了这个奇怪的错误

an unknown error occurred, please check the server response

这是我的代码:

func uploadVideo(videoURL: URL)
    {
        let storage = Storage.storage()
        let storageRef = storage.reference()
        let videoRef = storageRef.child("rPosts/\(uid!)/\(fileID)")

        let metadata = StorageMetadata()
        metadata.contentType = "video/quicktime"
        
        videoRef.putFile(from: videoURL, metadata: metadata)
        { (metaData, error) in
            guard error == nil else
            {
                self.errorLabel.text = error!.localizedDescription
                //error happens here :(
                return
            }
            print("works")
        }
    }

这里是使用它的地方:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
    {
        if let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL
        {
            uploadVideo(videoURL: url)
            uploadedYet = true
        }
        
        videoPickerController.dismiss(animated: true, completion: nil)
    }

使用 Firebase 的 putData: 方法上传您的数据,

这样试试:

  func uploadVideo(videoURL: URL)
    {
        let storage = Storage.storage()
        let storageRef = storage.reference()
        let videoRef = storageRef.child("rPosts/\(uid!)/\(fileID)")
        
        let metadata = StorageMetadata()
        metadata.contentType = "video/quicktime"
        
        var videoData : Data = Data()
        
        do
        {
            videoData = try Data(contentsOf: videoURL)
        }
        catch
        {
            print(error.localizedDescription)
            return
        }
        
        videoRef.putData(videoData, metadata: metadata) { (metaData, error) in
            guard error == nil else
            {
                self.errorLabel.text = error!.localizedDescription
                //error happens here :(
                return
            }
            print("works")
            
        }
    }