0
votes

After uploading a file in Firebase storage I want to call a firebase cloud function from my web app which will validate the file data I sent to it, and then store it in the real-time database.

I am calling the function using the following code:

var fileRef = 'Files/' + user.uid + '/' + fileId + '/' + file.name;
var storage = firebase.storage().ref(fileRef);
//Upload the file
//..................
// After uploading the file
storage.getMetadata().then(function(metadata) {
    var date = metadata.timeCreated.toString();
    var storage = firebase.storage().ref('Files/' + user.uid + '/' + fileId + '/' + file.name);

    storage.getDownloadURL().then(function(url){
        var saveFileData = functions.httpsCallable('saveFileData');
        saveFileData({
            fileId: fileId,
            fileRef: fileRef,
            fileData: {
                Uploader: user.uid,
                Title: title,
                FileName: file.name,
                Size: fileSize,
                DownloadURL: url,
                UploadDate: date,
            }
        }).then(function(){
            // Do something
        });
    });
});

I want to validate values of FileName, Size, DownloadURL and UploadDate in the cloud function based on the following conditions:

  • fileRef contains an existing file from the firebase storage.
  • FileName matches with the name of the file represented by fileRef.
  • Size matches with the size of the file represented by fileRef.
  • UploadDate matches with the creation date of the file represented by fileRef.
  • DownloadURL contains the download link of the file represented by fileRef.

What will be an appropriate way to do this from Firebase cloud function?

1
Instead of calling Cloud Functions from your app, trigger it directly from the Storage write. See this page for examples of the data available: firebase.google.com/docs/storage/extend-with-functionsFrank van Puffelen
But how will I get metadata from cloud function?Sadman Rizwan
Ah, I see. Your saveFileData writes to a Cloud Function, which then writes to the Realtime Database. This means you'll need to read it from the realtime database in your Cloud Function. You'll use the Firebase Admin SDK for this, as shown here: stackoverflow.com/questions/43650472/… and here: stackoverflow.com/questions/43913139/…Frank van Puffelen

1 Answers

2
votes

You can use storage triggers, Deploy in Firebase Cloud Functions,

In the object you will get all the metadata for the uploaded file as like here

exports.generateMetaForUpload = functions.storage
  .object()
  .onFinalize(object => {
    console.log(object);
       //do whatever you need to verify
  });