Firebase:如何将视频存储在存储中,然后将视频URL存储在数据库中? [英] Firebase: How to store Videos in storage and then store video URL in database?

查看:68
本文介绍了Firebase:如何将视频存储在存储中,然后将视频URL存储在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am using Firebase for the first time. I have read that you should store videos in Storage and then store that unique URL in their Database. How would I take this approach? For example if a user request a specific video to play how would I grab the URL from the database and then with that url pull the video out of the database?

感谢您的帮助,请原谅我对Firebase的经验不足.

Thank for the help and excuse my inexperience with Firebase.

推荐答案

零在Google I/O上进行的应用对话附带以下代码:

// 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设置为文本字段:

This takes the image that was selected in the image picker, uploads it to Firebase Storage and then sets the resulting download URL for that image into a text field:

// 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 方法然后将文本从文本框中发送到数据库.

The sendMessage method then sends the text from the text box to the database.

该最小示例的完整代码在此要点中.

Full code for that minimal example is in this gist.

这篇关于Firebase:如何将视频存储在存储中,然后将视频URL存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆