skip to Main Content

I’m trying to upload video that user chooses (through uiimagepickercontroller), to Firebase storage, but I’m getting this strange error within my localizedDescription

an unknown error occurred, please check the server response

Here’s my code:

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")
        }
    }

Here is where it’s used:

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

2

Answers


  1. Use Firebase’s putData: method to Upload your Data,

    Try it this way:

      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")
                
            }
        }
    
    Login or Signup to reply.
  2. Check your Firebase Storage settings, I changed settings with ;

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

    And fixed it .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search