How do I synchronize Firebase Storage and Database? - android

I have yet to find any good documentation to tell me how to manage this issue. Is there any guidance someone can reference for this issue?
For an Android app, I have started using Google Firebase Storage along with my realtime Database, and I have a question on how to ensure file uploads and database updates happen in the proper order.
Previously, I was encoding an audio file to a string and uploading it to FB Database along with other related data as a single hash map to ensure consistency. The related data that is being stored is a POJO relating to what users can access the audio, the path where the audio is stored in the database, and more.
My new set up is first to upload the audio file to FB Storage, and, once the file is uploaded, trigger a FB Database update for the related data. Once the database listener indicates completion, the app moves to a different activity.
I am concerned that this two step process is unstable (user leaves the activity before both tasks are done, Storage upload is interrupted and Database update never occurs, etc.). If the Database information isn't updated, the other users won't be notified that the audio file is online and they won't have the path to reference it stored in the database.
What is the proper order for structuring simultaneous Storage and Database uploads in the new Google Firebase?
Thanks,
Andy

Not sure why there are no solid answers for this yet. Anyway, I have faced very similar problems and eventually resorted with nested-updates using completion blocks (exactly how you described in your question).
Very recently, Firebase introduced cloud functions. I am utilizing this to watch the files that are uploaded, and then create a metadata entry into FB Database with the cloud function.
Basically, I use childByAutoId() to generate a key which is used to name the file. For example, profile pictures are saved at "gs://bucket-id/profile_pictures/somerandomkey.jpeg". Then I create a metadata entry into my FB database with cloud functions by utilizing the key from the filename.
I am not sure if there is a better way, but using cloud functions surely seems like a better approach than the nested-updates approach.

You can upload image using Firebase Storage, in success you get a downloadable URL of that image. Then put all the values in the custom object including that image URL and save that object in RealTime Database of Firebase.
And you can use the image URL by retrieving object from RealTime Database.

Related

How to keep firebase realtime database in sync with firebase storage, the standard approch to this problem of two nested queries?

How to keep firebase realtime database in sync with firebase storage? I am trying to store images in firebase storage.
The current scenario
a POJO (which contain 3 images placeholder link) is saved in the realtime database.
Then the 3 images are uploaded in firebase storage and on successful upload the respected placeholder links in the realtime database are updated.
Now the problem is that sometimes when the user deletes the project in between the POJO is deleted from firebase but the links are still updated.
Is there a better way of doing this ? or is there any standard approch? Thankyou for your time.
There is not a standard approach to this, and it can't be solved by simple configuration. You will have to get creative and implement something that meets your specific needs.
One thing you can do is use Cloud Functions triggers to mirror and delete data between the two products, so that one data in one is added, modified, or deleted, that change is reflected in the other product. It would take at least two triggers, one database trigger, and one storage trigger, to make sure changes in either product are mirrored to the other product. You might also need some additional process to remove or correct invalid data periodically, if that is a concern.

Firebase Real-Time database vs Storage

Im using Real-Time database to storage my users Profiles.
Each of the profiles can contain multiple rooms, where each of them contain their own picture creating a quite complex structure.
Here an example:
To make it easier to store the pictures to the corresponding profile, and room I am changing my pictures Bitmap in android to a String before I parse the object into the Database, and then when I get the object back I transform the String back to the Bitmap.
I was just wondering if this comes with any down cost in the future. Or if this implementation is safe where we put more data in the databases.
With your current database structure as-is, you will run into problems.
With the Realtime Database and this structure, everytime you request "user/SOME_ID", you will download all of the data below it - including your serialized images. Consult the database structure guide for information on how to flatten your data out so this doesn't occur.
Furthermore, I would recommend making use of Cloud Storage for Firebase to store your images in their native binary format rather than serializing to Base64 taking up ~30% more space. Like the RTDB, storage can be secured with rules if you store the files in structured locations like "user/SOME_ID/roomImages/ROOM_ID/..." or "roomImages/ROOM_ID/..."
I think that's not safe and bad implementation because u save users data in a third party service even without any encryption rather than storing it in your back-end which you have full control on it.
if I was A user for your app I didn't like that but if this app just for testing something there is no problem.

Proper Way to display images from Firebase Storage

From what I´ve read from the official firebase documentation and after watching this firecast my understanding is that in order to display images on a client there are two approaches:
The first is server based,using functions and writing the SignedUrls to the database.
The second is client based using StorageReference and pointing to the desired path in storage.
I have decided to follow the second approach due to the fact that the Firebase client SDK gives you the ability to directly query a storage photo for additional useful information about the photo (creation date,metadata etc) without the need of creating additional entries in the database
(like in the SignedUrl case by using functions.storage.ObjectMetadata).
My questions are:
1) can the bucket name or the full internal photo path be used in the client code without any security risks?
The path form may be:
gs://myapp.appspot.com/bucket_folder/username/photoname.PNG (non-default bucket)
2) Are there any drawbacks by using the client SDK method over the server-produced SignedUrls?
There are two ways to access items in Cloud Storage through the Firebase SDK:
By using the Firebase SDK methods to access the data.
By using the download URL.
When you use the download URL, the user doesn't have to be signed in. But the user can only ever read the file and (as you discovered) will only have access to the raw payload of the file, not the metadata.
When you use the other methods of the Firebase SDK, your access is controlled by the security rules. So your user may have to sign in.

Upload to database and storage at the same time in Firebase

In my android app I am using client side fan-out to upload data to multiple places in database at the same time to avoid problems for example in case of lost internet connection. Here is example code from Firebase blog:
Map updatedUser = new HashMap();
newPost.put("name", "Shannon");
newPost.put("username": "shannonrules");
Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
Map fanoutObject = new HashMap();
fanoutObject.put("/users/1", updatedUser);
fanoutObject.put("/usersWhoAreCool/1", updatedUser);
fanoutObject.put("/usersToGiveFreeStuffTo/1", updatedUser);
ref.updateChildren(fanoutObject); // atomic updating goodness
But now I would also like to upload image file to storage alongside that data. Is that posiible to do that at the same time?
I don't think you can synchronize calls to Firebase Database and storage. The documentation does not mention it anywhere.
But just so you know: Firebase will save any requests made by updateChildren() in the cache if they cannot be sent due to loss of connection. When the user reconnects, Firebase will then dispatch all the cached requests in the same order that they were saved.
One way to do these two tasks concurrently would be to first determine your file path in Storage, and kick off the upload, then also use that file path to update the database. That's about as good as you'll get.
However, most of the time you want to store the "download URL" of the uploaded file in the database, instead of the path in the storage bucket. This makes it easier for clients to go directly to the content without having to "look up" the download URL first. In this case, you really do need to wait for the storage upload to complete first, get the download URL from the UploadTask, then update the database with that string. You can't know the download URL before the upload completes.

Getting download url of multiple files inside Firebase Storage

I am creating an Android App where Firebase is the backed. In my app, I have some selected images that I have stored inside my Firebase storage by direct uploading (Without coding).
Now I have to view that images in my app through a recycle view. For that purpose, I have to get the download URL of all the images and put them into my Firebase real-time database programmatically so that I can access the URL to my app. Is there any methods available for that?
I have tried to iterate through storage, unfortunately there is no method for that.
There is currently no way to iterate a list of files in Firebase Storage using the Firebase SDKs. Instead, after you upload a file, you should also store its download URL in Firebase Realtime Database (or some other place) that you can query for the download URLs.
I was searching for a proper answer to this question for 2 days. What i found is, There is no proper API or Class in Firebase Storage for the same purpose. If you guys want to do the same effectively, get the files in your code programmatically and upload them to firebase storage through a loop. Through that you can get the download links after uploading of each files. Store it in the Real time Database of Firebase.
You can create a function that can can return a file. You can set up a /link url in the hosting to that function.

Categories

Resources