How to observe Firebase Storage Upload event

核能气质少年 提交于 2020-02-22 11:36:05

问题


I have an iOS app which uploads photos to Firebase Storage, and a web app which is connected to the same Firebase. Is there a way to observe changes in Storage from the web? Only the iOS device itself has access to the UploadTask when a photo is being uploaded, and I did not see an on event observer in the docs for Storage like there is for Database.


回答1:


Firebase Storage doesn't provide the same "sync" functionality as the Realtime Database (imagine what it would be like trying to sync 1GB objects...). You'll want to use the database to synchronize the metadata (such as download URLs, etc.) on file uploads like so:

// Upload file to Firebase Storage
storageRef.putData(imageData).observeStatus(.Success) { (snapshot) in
  // When the image has successfully uploaded, get it's download URL
  let url = snapshot.metadata?.downloadURL()?.absoluteString

  // Write data to the realtime database
  dbRef.child("photos").childByAutoId().setValue(["name": snapshot.metadata?.name, "url": url])
}

...

dbRef.child("photos").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in 
  // snapshot contains the name and URL of the uploaded file
});

Code is from Zero to App, a talk we did at I/O 2016, which details this a little better



来源:https://stackoverflow.com/questions/37552408/how-to-observe-firebase-storage-upload-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!