首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase:如何将视频存储在存储中,然后将视频URL存储在数据库中?

Firebase:如何将视频存储在存储中,然后将视频URL存储在数据库中?
EN

Stack Overflow用户
提问于 2016-06-19 04:12:23
回答 2查看 6.7K关注 0票数 7

所以我是第一次使用Firebase。我读到过,您应该将视频存储在存储中,然后将唯一的URL存储在他们的数据库中。我该如何采用这种方法呢?例如,如果用户请求播放特定的视频,我如何从数据库中获取URL,然后使用该url将视频从数据库中拉出?

感谢您的帮助,并原谅我对Firebase缺乏经验。

EN

回答 2

Stack Overflow用户

发布于 2016-06-19 05:46:45

来自Zero to App talk at Google I/O的代码如下:

代码语言:javascript
复制
// pragma mark - UIImagePickerDelegate overrides
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

  // Get local file URLs
  guard let image: UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
  let imageData = UIImagePNGRepresentation(image)!
  guard let imageURL: NSURL = info[UIImagePickerControllerReferenceURL] as? NSURL else { return }

  // Get a reference to the location where we'll store our photos
  let photosRef = storage.reference().child("chat_photos")

  // Get a reference to store the file at chat_photos/<FILENAME>
  let photoRef = photosRef.child("\(NSUUID().UUIDString).png")

  // Upload file to Firebase Storage
  let metadata = FIRStorageMetadata()
  metadata.contentType = "image/png"
  photoRef.putData(imageData, metadata: metadata).observeStatus(.Success) { (snapshot) in
    // When the image has successfully uploaded, we get it's download URL
    let text = snapshot.metadata?.downloadURL()?.absoluteString
    // Set the download URL to the message box, so that the user can send it to the database
    self.messageTextField.text = text
  }

  // Clean up picker
  dismissViewControllerAnimated(true, completion: nil)
}

这将获取在图像选取器中选择的图像,将其上载到Firebase存储,然后将该图像的结果下载URL设置到文本字段中:

代码语言:javascript
复制
// Send a chat message
func sendMessage(sender: AnyObject) {
  // Create chat message
  let chatMessage = ChatMessage(name: self.username, message: messageTextField.text!, image: nil)
  messageTextField.text = ""

  // Create a reference to our chat message
  let chatRef = database.reference().child("chat")

  // Push the chat message to the database
  chatRef.childByAutoId().setValue(["name": chatMessage.name, "message": chatMessage.message])
}

然后,sendMessage方法将文本框中的文本发送到数据库。

这个最小示例的完整代码在this gist中。

票数 3
EN

Stack Overflow用户

发布于 2016-12-06 15:49:14

这是我将视频存储到Firebase存储中的解决方案。给斯威夫特3。

代码语言:javascript
复制
func uploadVideo(_ path: URL, _ userID: String,
                 metadataEsc: @escaping (URL, FIRStorageReference)->(),
                 progressEsc: @escaping (Progress)->(),
                 completionEsc: @escaping ()->(),
                 errorEsc: @escaping (Error)->()) {

    let localFile: URL = path
    let videoName = getName()
    let nameRef = StorageHandler.videosRef.child(userID).child(videoName)
    let metadata = FIRStorageMetadata()
    metadata.contentType = "video"

    let uploadTask = nameRef.putFile(localFile, metadata: metadata) { metadata, error in
        if error != nil {
            errorEsc(error!)
        } else {
            if let meta = metadata {
                if let url = meta.downloadURL() {
                    metadataEsc(url, nameRef)
                }
            }
        }
    }

    _ = uploadTask.observe(.progress, handler: { snapshot in
        if let progressSnap = snapshot.progress {
            progressEsc(progressSnap)
        }
    })

    _ = uploadTask.observe(.success, handler: { snapshot in
        if snapshot.status == .success {
            uploadTask.removeAllObservers()
            completionEsc()  
        }
    })
}

func getName() -> String {
        let dateFormatter = DateFormatter()
        let dateFormat = "yyyyMMddHHmmss"
        dateFormatter.dateFormat = dateFormat
        let date = dateFormatter.string(from: Date())
        let name = date.appending(".mp4")
        return name
    }

调用者:

代码语言:javascript
复制
StorageHandler.shared.uploadVideo(myFileURL, myUserID, metadataEsc: { (url, ref) in
    print("url = \(url)") // here is the URL you can then store into your Firebase tree
    print("ref = \(ref)")
}, progressEsc: { progress in
    print("progress = \(progress)")
}, completionEsc: {
    print("done")
}, errorEsc: { error in
    print("*** Error during file upload: \(error.localizedDescription)")
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37901213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档