How to upload image to firebase storage and save reference in the database in single transaction?

两盒软妹~` 提交于 2021-01-27 23:46:19

问题


I would like to upload image to firebase storage and save link to it in the firebase database. Saving link to database can fail for whatever reason and then I have unused, unlinked image in the storage. Is it possible to somehow ensure that after upload link to the image is saved in the database?


回答1:


This is another approach other than using transaction. You can use Firebase Cloud Functions Storage Trigger. You can write a cloud function to get the download URL of the uploaded image and then push it to firebase database.

Check this video by firebase about Cloud Function Storage Trigger




回答2:


How about this pattern.

  1. Save the file in cloud storage in a bucket that has a lifecycle of 1 day. We can call it a temporal bucket.
  2. Save the url into your database.
  3. Have in place a cloud function that will move the object from the temporal bucket to a permanent one. The function can replace the url from the database once the object is in the permanent bucket.

So, in the case you cannot save the url to the database, that object will get deleted after 1 day




回答3:


Yes, you can use addOnSuccessListener on your task like this:

uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        Uri downloadUrl = taskSnapshot.getDownloadUrl();
        firebaseRef.child("urls").child("urlId1").setValue(downloadUrl.toString);
    }
});

This means that only if the upload succeeded, you are writting the url to the database.




回答4:


I would suggest you wait for the image to get uploaded in the firebase, get the URL from the storageRef and then push the Url into the database like below. I'm using javascript method, I hope you will get an idea on how to achieve your need.

<input type="file" value="" name="Photo" class="btn btn-info" id="fileButton" accept="image/*"> //html Code

In js file, add event listener like

var fileButton = document.getElementById('fileButton');
fileButton.addEventListener("change",function(e){
   var file= e.target.files[0];
   //create storage ref to the firebase storage
   var storageRef = firebase.storage().ref('your Path Name').child(file.name);
   var task = storageRef.put(file);
   task.on("state_changed",function(snapshot){
      var percentage= snapshot.bytesTransferred/snapshot.totalBytes) *100;
      if(percentage==100){
          storageRef.getDownloadURL().then(function (url) {
             // You will get the Url here.
            var firebaseRef=firebase.database().ref("Your path Name);
            firebaseRef.push(url).then(function(){
              alert("Image Uploaded and also updated to the database");
           });
          });
      }
   });
});

I hope this helps you, this happens in a single transaction.



来源:https://stackoverflow.com/questions/47012372/how-to-upload-image-to-firebase-storage-and-save-reference-in-the-database-in-si

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