0

I have been trying to implement a function through which I can upload videos to Firebase storage using videos url

      static func savePostVideo(userId: String, caption:String,postId: String, url:URL, metadata: StorageMetadata, storagePostRef: StorageReference, onSuccess: @escaping()->Void, onError: @escaping(_ errorMessage: String)->Void) {
       
       
       
            storagePostRef.putFile( from: url, metadata: nil) { (storageMetadata, error) in
       
                   if error != nil {
                       onError(error!.localizedDescription)
                       return
                   }
                   storagePostRef.downloadURL { (url,error) in
                       if let metaImageUrl = url?.absoluteString {
                       let firestorePostRef = Ref.FIRESTORE_MY_POSTS_DOCUMENT_USERID(userId: userId).collection("userPosts").document(postId)
                       let post = Post.init(caption: caption, likes: [:], location: "", ownerId: userId, postId: postId, username: Auth.auth().currentUser!.displayName!, avatar: Auth.auth().currentUser!.photoURL!.absoluteString, mediaUrl: metaImageUrl, date: Date().timeIntervalSince1970, likeCount: 0)
                       guard let dict = try? post.toDictionary() else {return}
                       
                       firestorePostRef.setData(dict) { (error) in
                           if error != nil {
                               onError(error!.localizedDescription)
                               return
                           }
                           Ref.FIRESTORE_TIMELINE_DOCUMENT_USERID(userId: userId).collection("timelinePosts").document(postId).setData(dict)
                           Ref.FIRESTORE_COLLECTION_ALL_POSTS.document(postId).setData(dict)
                           onSuccess()
                       }
                       
                   }
           }
   }
      

However, whenever I run the code I keep getting this error on the debugger:

error requesting a NSURLSessionUploadTask from background.

I have implemented a similar method to upload photos and it works perfectly fine but unfortunately I couldn't do the same with this one. I can provide additional information if needed

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • There is no way to write data to Cloud Storage directly from a URL. You will first have to download the data from the URL, and then upload it to Storage. Alternatively you can also store the external URL in your database, if that fits your use-case. See https://stackoverflow.com/q/37686213, https://stackoverflow.com/q/58980988 – Frank van Puffelen Oct 03 '20 at 23:14
  • It would have been nicer if you could provide an example of the first option. – Mohamed Hassan Oct 04 '20 at 11:41
  • Downloading data from a URL you mean? https://stackoverflow.com/questions/28219848/how-to-download-file-in-swift – Frank van Puffelen Oct 04 '20 at 14:23
  • yes, downloading data from a URL. sorry I am just a beginner – Mohamed Hassan Oct 05 '20 at 01:39
  • The link in my previous comment shows one way to download the data from a URL in Swift. since that seems to be what is missing from your current code. If you're having a hard time making that **download** work for you, post a new question with the [minimal code that reproduces where you got stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Oct 05 '20 at 04:05

0 Answers0