0

I am trying to create a cloud function that stores a file into storage. Oringally I had this on the front-end, but have decieded that I need this done as a cloud function. I am struggling with how to do this. my function looks like:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

exports.exampleFunction = functions.https
.onCall((data, context) => {
      const someData = {
        some1:"abc",
        some2: "def"
      };

      let newKey;
      return admin.database()
          .ref("/level1/"+data.dataVar1+"/level2/"+data.dataVar2+"/random")
          .push(someData)
          .then((snapshot)=>{
  
            newKey=snapshot.key;
            return admin.database()
                .ref("/level1/"+data.dataVar1+"/level2/"+data.dataVar2)
                .once("value");
          })
          .then((snapshot)=>{
            const file = data.file;

//Error here

return admin.storage().ref() 
                .child(snapshot.val().randomVar+"/"+newKey)
                .put(file); 

         
          })
          .catch((error)=>{
            console.log(error);
            console.log(error.message);
            return error;
          });
    });

However, when I run this I get the following error:

admin.storage(...).ref is not a function

How can I upload the file to storage?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Charmalade
  • 645
  • 1
  • 6
  • 14

1 Answers1

3

The storage() method returns an instance of Storage class that does not have a ref() method. You need to get reference to the File and then save() it as shown below:

return admin.storage().bucket() // <-- default bucket
  .file(snapshot.val().randomVar + "/" + newKey)
  .save(file)
  .then(() => {
    console.log("File uploaded")
    return
  })
  .catch((e) => console.log(e));
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Thanks for your reply. I have updated, my code but I am now getting the error: admin.storage(...).bucket(...).child is not a function – Charmalade Oct 26 '22 at 09:51
  • 1
    @Charmalade it should be `file()` and not `child()`. Updated the answer – Dharmaraj Oct 26 '22 at 09:52
  • Ah, good spot. I am still having issues after updating. Now the function provides the following error: The "chunk" argument must be one of type string or Buffer. On my front-end I have an input of type 'file'. This input referenced by a variable of name 'randomFile'. I pass randomFile.files[0] to the cloud function. This is then accessed as data.file in my cloud function. – Charmalade Oct 26 '22 at 10:58
  • 1
    @Charmalade you can't just pass a blob/file in a JSON body that way. You can either convert it to base64 on frontend and pass to CF or use a multi part form. Also a Cloud Function can have a max request body of 10 MB so it might not work for large files. – Dharmaraj Oct 26 '22 at 11:09
  • 1
    Uploading a file to CF sounds like a different question than the original SDK related one. It might be best to post a new question with relevant details so more people can look into the new issue and mark this one as resolved. – Dharmaraj Oct 26 '22 at 11:10
  • Thanks for your help. I opened a new question here: https://stackoverflow.com/questions/74207371/uploading-a-file-from-front-end-to-firebase-storage-via-cloud-functions – Charmalade Oct 26 '22 at 11:58