微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将视频上传到Firebase并下载URL

如何解决将视频上传到Firebase并下载URL

我正在尝试将视频上传到Firebase Storage,然后下载URL,以便将其存储在Firestore数据库中。以前(iOS 12)可以正常工作,但现在我没有收到任何错误消息,但视频没有上传,显然网址没有下载。我本来以为问题是我没有使用主线程,但是从主线程执行操作时仍然没有上传。我的存储权限是最宽松的,因此我认为这不是问题。

    // Local file to upload
    let localFile = self.videoURLPath!
    
    // Create the file Metadata
    let Metadata = StorageMetadata()
    Metadata.contentType = "mp4"

    // Create a root reference
    let storage = Storage.storage()
    let storageRef = storage.reference()
    
    // Create a reference to 'endoscope folder'
    let firstRef = storageRef.child("Endoscope")
    let finalRef = firstRef.child("\(filename).mov")
    
    // Upload file and Metadata to the object 'images/mountains.jpg'
    print(localFile)
    print(filename)
    dispatchQueue.main.async {
        let uploadTask = finalRef.putFile(from: URL(fileURLWithPath: localFile),Metadata: Metadata,completion: {(Metadata,error) in
            if error == nil {
                print("Successful video upload")
                finalRef.downloadURL { (url,error) in
                    if url != nil {
                        self.downloadURL = url
                    }else {
                        print(error!)
                        return
                    }
                }
            } else {
                print(error?.localizedDescription as Any)
            }
            
        })
        
        uploadTask.observe(.resume) { snapshot in
            // Upload resumed,also fires when the upload starts
            print("observe status: resume")
        }
        
        uploadTask.observe(.pause) { snapshot in
            // Upload paused
            print("observe status: pause")
        }
        
        uploadTask.observe(.progress) { snapshot in
            // Upload reported progress
            let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
                / Double(snapshot.progress!.totalUnitCount)
            print(percentComplete)
        }
        
        uploadTask.observe(.success) { snapshot in
            print("Upload successful")
        }

        uploadTask.observe(.failure) { snapshot in
            if let error = snapshot.error as NSError? {
            switch (StorageErrorCode(rawValue: error.code)!) {
            case .objectNotFound:
              print("File does not exist")
              break
            case .unauthorized:
              print("Unauthorized to access this file")
              break
            case .cancelled:
              print("Upload cancelled")
              break

            /* ... */

            case .unkNown:
              // UnkNown error occurred,inspect the server response
              break
            default:
              // A separate error occurred. This is a good place to retry the upload.
              break
            }
          }
        }
        
        
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。