8
votes

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...

3

3 Answers

10
votes

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

3
votes

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
  })
-1
votes

For me, I had to call to Firebase Storage 2x. I'm using Java on an Android device to edit the metadata. First time is to upload the image. Second time is to set the image's metadata. Instructions to set the Metadata of a Storage file is here= https://firebase.google.com/docs/storage/android/file-metadata

"You can update file metadata at any time after the file upload completes by using the updateMetadata() method. "

Here's my functions:

    private void uploadImageToFBStorageAndFS(byte[] profilePic, final StorageUrlEstablished_CL storageUrlCallback) {
        String storage_directory =  //You get this
        StorageReference profileImageRef = FirebaseStorage.getInstance().getReference(storage_directory).child(final_filename);
    
   //1st time, upload the image/bytes.
        if (profilePic != null) {
            profileImageRef.putBytes(profilePic).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Task<Uri> result = taskSnapshot.getMetadata().getReference().getDownloadUrl();
                    result.addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            updateImageMetadata(profileImageRef);
    
                            String urlWhereProfilePicIsStored = uri.toString();
                         }
                    });
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                   //Error handling
                }
            });
        }
    }

    private void updateImageMetadata(StorageReference profileImageRef){
        //Some devices, like the Asus tablet, doesn't upload good meta-data with the image.
        // Create file metadata including the content type
        StorageMetadata metadata = new StorageMetadata.Builder()
                .setContentType("image/png")
                .setCustomMetadata("myCustomProperty", "Rent App")
                .build();

        // Update metadata properties
        profileImageRef.updateMetadata(metadata);
    }