I always have a hard time when I'm uploading images to my Firebase project using the firebase-admin
package.
Here is what I get when I upload images as an admin user, using my app's admin section, which uses the regular firebase
JS package.
This is the code I use:
const metadata = {
cacheControl: "public,max-age=31536000,must-revalidate"
// contentType: "image/jpeg" // THIS IS AUTO INFERRED BY FIREBASE
};
const storageRef = firebase.storage().ref(`${directory}/${fileName}`);
const uploadTask = storageRef.put(file,metadata);
uploadTask.on("state_changed",
function progress() {...},
function error() {...},
function complete() {...}
);
And this is what I get on Firebase storage console: a nice preview with links and a download token. Even though the cacheControl
metadata is not displayed, it was set, 'cause it's visible when I visit the image's URL on my browser.
Now I'm writing an admin
script to upload some images that I have on my local machine to my Firebase storage.
This is the code:
async function uploadImage() {
admin.storage().bucket().upload(
`./temp/${imageLocation}`, // THIS IS MY LOCAL PATH
{
destination: imageLocation, // THIS IS THE PATH FOR THE STORAGE
}
);
console.log(`Uploaded: ${imageLocation}`);
}
Everything works fine, the file was indeed uploaded, but this is what I get on Firebase storage console:
QUESTION
How can I use the firebase-admin
in a way that I get the same consistent result as when I'm uploading an image using the firebase
JS package on the browser?
Can I use the regular firebase
package in my NodeJs admin script? I think I would have to authenticate as an admin
user before making the firebase.storage().ref().put()
request, since all storage paths are protected with allow write: if request.auth.token.admin == true;
.