Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom data to a firebase storage upload?

I'm uploading files to firebase storage like so:

var storageRef = firebase.storage();
                var fileRef = storageRef.ref(file.name);
                fileRef.put(file)
                    .then(function (snapshot) {
                        console.log('Uploaded a blob or file!');
                        window.URL.revokeObjectURL(file.preview);
                    })

After the upload I have a firebase storage trigger:

export const processUploadedFile = functions.storage.object().onChange(event => {
}

What I want to do is upload some additional information with the original upload so that the processUploadedFile knows what to do with it (for example extract the file, move it to a special directory, etc, etc).

I tried using metadata like so:

        var newMetadata = {
            customMetadata: {
                "Test": "value"
            }
        }

fileRef.put(file, newMetadata)

But on the cloud storage trigger function I don't know how to get the metadata, I logged out fileMetaData like so:

file.getMetadata().then((metaData)=>console.log(metaData))

But did not see my metadata anywhere in there (or in fileMetaData[0].metadata which returned undefined)

Not sure how I can achieve this...

like image 225
meds Avatar asked Jan 01 '18 07:01

meds


2 Answers

I believe there are some properties that cannot be changed as they are not writeable. However, if you indeed want to add a custom data to firebase storage, you can set custom metadata as an object containing String properties. For example:

 var myCustomMetadata = {
    customMetadata : {
     'file_name': 'this is the file name'
     }
   }

In the case above, the file_name is the custom metadata that you want to create.

After creating a reference to the file in the firebase storage, you can then call the updateMetadata() method on the reference.

For example:

Get the reference to an image file using downloadUrl:

  var getRef = firebase.storage().refFromURL(imageUrl);

Use the reference to update the metadata:

getRef.updateMetadata(myCustomMetadata).then(()=>{
    //do other things
  })
like image 183
franc Avatar answered Oct 19 '22 23:10

franc


I think providing file meta info will do the trick. Here is the reference. Firebase Storage File metadata. You can pass custom parameters for the file with customMetadata. For instance :

customMetadata: {
    'actionType': 'ACTION_CODE',
    'action': 'do something info'
}

You can access this metadata with storage trigger and take the action accordingly. Here is how you can achieve that Automatically Extract Images Metadata

like image 25
mark922 Avatar answered Oct 19 '22 23:10

mark922