'Convert local url to type of data and upload to a server swift

How do I capture the previewURL data so I can upload it to my server?

In other words, my goal is to take the local file url

@Published var previewURL: URL?
// file://123-123-123.mov

then convert it to a Data type:

@Published var movieData = Data(count: 0)

and then upload it to my AWS server

   func saveVideo() async {
        
        let uploadUrl = URL(string: postUploadData.url!)!
        var requestToUpload = URLRequest(url: uploadUrl)
        requestToUpload.httpMethod = "PUT"

        requestToUpload.httpBody = self.movieData
        do {
            let (data, _) = try! await URLSession.shared.data(for: requestToUpload)
  
        } catch {
            debugPrint("Error loading \(url): \(String(describing: error))")
        }
   
    }

Here is the callback when I finish recording the video:

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
        if let error = error {
            print("FILE OUTPUT ERROR ----->", error.localizedDescription)
            return
        }
        
        // CREATED SUCCESSFULLY
       
        self.recordedURLs.append(outputFileURL)
        

        self.previewURL = outputFileURL
   
    }


Solution 1:[1]

Add this code in the fileOutput function:

let data = try Data(contentsOf: self.previewURL!)
    print("DATA FOR MOVIE FILE ----->", data)    
} catch {
    print(error)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leo Dabus