Search code examples
javascriptfirebasegoogle-cloud-platformgoogle-cloud-storagefirebase-storage

Creating a file and settimng metadata at the same time in Firbase Storage


i am currently working on a react app which uses Firebase Storage to store files. My problem is: i want to create a file and attach metadata to it before storing it on Firebase storage; as far as i know, now i have to

  1. save the file in the storage creating the file ref
  2. take the ref of that file, set metadata and upload it.

is there a way to make these 2 steps into 1?

Thanks in advance for your time


Solution

  • As explained in the Cloud Storage for Firebase doc, "when uploading a file, you can also specify metadata for that file".

    // Create file metadata including the content type
    var metadata = {
      contentType: 'image/jpeg',
    };
    
    // Upload the file and metadata
    var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);