I am trying to upload a video to firebase storage that is saved in FileManager as a URL looking like this file:///Users/admin/Library/Developer/CoreSimulator/Devices/D9BA697D-2ACA-4C4F-BE64-9004016A8500/data/Containers/Data/Application/1133A56A-B92F-4A8A-B9D9-0A8180607B93/Documents/videorecording.mp4
. I tried uploading it like this:
private func persistVideoToStorage(id: String, video: URL) {
let ref = FirebaseManager.shared.storage.reference(withPath: id)
ref.putData(video, metadata: nil) { metadata, err in
if let err = err {
print(err)
return
}
ref.downloadURL { url, err in
if let err = err {
print(err)
return
}
guard let url = url else { return }
}
}
}
But I need to upload the type of Data and cannot convert URL to Data.
CodePudding user response:
Try something like this:
do {
let videoData = try Data(contentsOf: video)
//....
ref.putData(videoData, metadata: nil)
//....
} catch {
print("-- Error: \(error)")
}