Alex
Alex

Reputation: 1849

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

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?

Upvotes: 2

Views: 8174

Answers (4)

ajorquera
ajorquera

Reputation: 1309

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

Upvotes: 3

MuruGan
MuruGan

Reputation: 1420

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.

Upvotes: 2

Joseph Varghese
Joseph Varghese

Reputation: 609

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

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

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.

Upvotes: 2

Related Questions