I have started to use firebase (Firebase for web) for my backend. I can handle Firebase Realtime Database quite well, but I do not clearly understand how Firebase Storage (for images, videos etc.) works.
Basically I would like to have some list of news on my website each item with some small picture. So I uploaded these pictures to Firebase Storage, but how can I access their URL (need it for my <img/>
src attribute)?
To delete a file, first create a reference to that file. Then call the delete() method on that reference, which returns a Promise that resolves, or an error if the Promise rejects.
Once you have a reference, you can download files from Cloud Storage by calling the getBytes() or getStream() . If you prefer to download the file with another library, you can get a download URL with getDownloadUrl() .
The Firebase Storage token does not expire (see Stack Overflow). Therefore, without any other modifications, our downloadUrl also never expires and remains available. But, is it possible in the Firebase Console to invalidate a specific URL.
Firebase Storage works in a similar way when extracting data. Though there are different ways to retrieve. One of the most important factor is how you save your data. According to which you will be able to retrieve them.
Here:
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'users/me/profile.png' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } }); // Alternatively way to get download URL storageRef.child("users/me/profile.png").getDownloadUrl().getResult();
The location of child is very crucial and that should be a part of your login.
For example in a news structure, it can be:
storageRef.child("newsID/articleimage/image.jpg")
newsID - Unique id for each article. IMPORTANT! when you create your article in order to get separate images. and that, "image.jpg" can be downloaded various ways as explained here: Documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With