1
votes

well in my case I have a list of Url and I want to download each and every file from those urls and organise it in firebase storage bucket, my problem is I am unable to make folders in firebase storage bucket through nodejs javascript/typescript.

well firebase storage offers ref() and child method to upload files inside child folder (see this) but firebase only offers those method for firebase client libraries, it is not that we can not use client library in nodejs but they have made some namespaces hidden when you connect firebase client library in nodejs and storage is one of them (see this).

I am happy they have considered frontend and backend separately because of this very reason that front and backend have whole different scenario for security and use cases, so what they have really written to use in nodejs is firebase admin and I cannot see ref and child method in official documentation which they have said is this not any other way to name the file I am uploading nor any method for making folders to go child directories, when I upload a file from my computer it get saved in the bucket root with the same name as the filename it was in my computer, even though I can make folders from firebase console manually but it will not fulfill my requirement for sure there should must be any way to make folders in programmatically.

I also tried using google cloud storage library const {Storage} = require('@google-cloud/storage'); but it turned out firebase admin and gogole cloud library shares the same document and have same interface at least in upload file part.

well I have spent my day (well night too since it is 4:46am) trying different libraries and digging into their documents which I also found little unorganised and lack of code examples.

any help would be appreciated, my code snippet so far is following which is from their doc and uploading file correctly:

import "firebase/firestore"

admin.initializeApp({
    credential: admin.credential.cert("./../path-to-service account-cert.json"),
    databaseURL: 'gs://bilal-assistant-xxxxx.appspot.com'
});
const quran_bucket = admin.storage().bucket("quran-bucket");

quran_bucket.upload("./my_computer_path/fatiha.mp3", {
    gzip: true,
    metadata: {
        cacheControl: 'public, max-age=31536000',
    }
}).then(uploadResponse => {
    console.log(` uploaded complete.`);
}).catch((reason: any) => {
    console.log("reason: ", reason);
})

All I wanted is to save the audio file in folder bucket, not in bucket root

1
Cloud Storage just doesn't have folders. You can't make one or delete one. What it does have is file paths that look like they have a folder structure, which helps you think about how your content is organized. It also helps you navigate your content in the console. But there are just no folders there. You can find a lot of discussions about this on Stack Overflow.Doug Stevenson
All that said, what is the problem with your code? Is there an error? What does it do other than what you expect?Doug Stevenson
problem is I want to save fatiha.mp3 in a folder such as audio/juz30/fatiha.mp3 in firebase storageInzamam Malik

1 Answers

7
votes

According to the API documentation, upload() takes an UploadOptions object as the second parameter. You will want to used the documented destination property of that object to specify the name of the file in Storage:

quran_bucket.upload("./my_computer_path/fatiha.mp3", {
    destination: 'audio/juz30/fatiha.mp3',
    gzip: true,
    metadata: {
        cacheControl: 'public, max-age=31536000',
    }
})

You probably don't want to bother the gzip an mp3, as it's already compressed and won't compress much further.