问题
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