Search code examples
firebasegoogle-cloud-platformgoogle-cloud-functionsfirebase-storagefirebase-admin

Firebase Admin SDK cant upload to storage bucket folder


I'm trying to upload a file using Firebase function, multer using this docs but it seems I can't upload to folder Firebase as shown here:

screenshot

I want to upload directly to my folder. I already tried this:

const bucket = storage.bucket("<my-bucket>.appspot.com/img_users/");

but it's still not working.


Solution

  • There is actually no concept of folder or subdirectory in Cloud Storage. The img_users/ "folder" you see in the Firebase console is actually generated from the file name of one or more files which have a file name composed as follows:

    img_users/<rest_of_the_file_name>

    The Firebase console creates "fake" folders based on the parts of the file name separated by some slashes (/). In other words, the console "makes it appear" that the above file resides in the img_users/ "folder", but to Cloud Storage, the object simply exists in the bucket and has the name shown above. You will find more detailed explanations in the doc.

    The consequence of this behaviour is that you should not include the "folder" name in the String you pass to the bucket() method, but include it when you create a file Reference.