我将视频上传到Firebase时会遇到“未能发出沙箱扩展”错误。我的图像上传正好很好,只有videos会引起问题。
func testUploadLocalVideo() {
if let localVideoURL = Bundle.main.url(forResource: "IMG_3787", withExtension: "MOV") {
print("Attempting to upload local video: \(localVideoURL)")
uploadVideoToFirebase(fileURL: localVideoURL)
} else {
print("❌ Local test video not found.")
}
}
func uploadImageToFirebase(image: UIImage) {
let storageRef = Storage.storage().reference()
guard let imageData = image.jpegData(compressionQuality: 0.75) else {
print("Error converting image to data")
return
}
let fileName = UUID().uuidString + ".jpg"
let imageRef = storageRef.child("images/\(fileName)")
imageRef.putData(imageData, metadata: nil) { metadata, error in
if let error = error {
print("Error uploading image: \(error.localizedDescription)")
return
}
imageRef.downloadURL { url, error in
if let error = error {
print("Error getting download URL: \(error.localizedDescription)")
return
}
if let downloadURL = url {
DispatchQueue.main.async {
self.selectedImages.append(image)
self.isMediaSelected = true
}
print("✅ Image uploaded successfully: \(downloadURL)")
}
}
}
}
func uploadVideoToFirebase(fileURL: URL) {
print("Starting video upload with fileURL: \(fileURL)")
// Create a temporary file URL within your app's sandbox
let tempFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileURL.lastPathComponent)
do {
// Copy the video file to the temporary location
try FileManager.default.copyItem(at: fileURL, to: tempFileURL)
print("✅ Video copied to temporary location: \(tempFileURL)")
// Upload the video from the temporary location
uploadVideoFromTempFile(tempFileURL: tempFileURL)
} catch {
print("❌ Error copying video file: \(error)")
}
}
func uploadVideoFromTempFile(tempFileURL: URL) {
// 1. Generate a unique filename using UUID
let fileName = UUID().uuidString + ".mov"
// 2. Create a reference to the video file in Firebase Storage
let storageRef = Storage.storage().reference().child("videos/\(fileName)")
// 3. Create a metadata object to store the video's content type
let metadata = StorageMetadata()
metadata.contentType = "video/quicktime"
// 4. Load video data
guard let videoData = try? Data(contentsOf: tempFileURL) else {
print("❌ Unable to create video data from temporary file")
return
}
// 5. Upload the video data directly using putData
storageRef.putData(videoData, metadata: metadata) { metadata, error in
if let error = error {
print("❌ Error uploading video: \(error.localizedDescription)")
} else {
// Get download URL from the storage reference
storageRef.downloadURL { url, error in
if let downloadURL = url?.absoluteString {
DispatchQueue.main.async {
self.selectedVideos.append(tempFileURL) // Store tempFileURL
self.isMediaSelected = true
}
print("✅ Video uploaded successfully: \(downloadURL)")
} else {
print("Video uploaded successfully, but download URL is not available.")
}
}
}
}
}
尽管尝试了各种解决方案,例如处理安全性的URL并将视频复制到临时位置,但错误仍然存在。这表明可能还有其他因素正在发挥作用,例如丢失的权利,并发问题,内存压力,Firebase SDK的问题,特定于设备的限制或干扰代码。也许我没有做任何正确的步骤(因为它没有起作用)。我的目标是使视频像图像一样上传到燃料库。
这个问题的可能重复。请确保检查一下,然后尝试此