I have a function which receives a file in this format through multer package.. Now I want to upload this file to firebase storage, for that I need it in a Blob
or in javascript file
format.
My function is-
const getFileURL = file => {
const storageRef = firebase.storage().ref();
let url = "";
let err = null;
const metadata = {
contentType: file.mimetype,
};
const uploadFile = storageRef.child(name).put(file);
uploadFile.on(
"state_changed",
function (snapshot) {},
function (error) {
console.log("errrroror", error);
return error;
},
function () {
uploadFile.snapshot.ref
.getDownloadURL()
.then(function (downloadURL) {
url = downloadURL;
return url;
});
}
);
};
File received in the above function:
{
fieldname: 'file',
originalname: 'file_name.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: './public/images',
filename: '85d68e32b2b18adbfc7d0f72b5746e43',
path: 'public/images/85d68e32b2b18adbfc7d0f72b5746e43',
size: 96966
}
Caller Function:
uploadImage = async (req, res, next) => {
try {
const url = getFileURL(req.files[0]);
return res.json({ location: url });
} catch (e) {
return res.json(e);
}
}
I'm getting error as :Firebase Storage: Invalid argument in 'put' at index 0: Expected Blob or File.