3

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.

4
  • can you show the caller function? Commented Oct 9, 2020 at 19:09
  • @ShivamSingla updated Commented Oct 9, 2020 at 19:12
  • Are you using mutler.single or mutler.array or mutler.fields ? Commented Oct 9, 2020 at 19:15
  • @ShivamSingla I'm using mutter.any(). getFileURL is receiving file properly, I don't think problem is in the calling function Commented Oct 9, 2020 at 19:17

1 Answer 1

-1

This should work

// ...snip
const blob = new Blob([file.buffer])
const uploadFile = storageRef.child(name).put(blob)
// ...snip

Reference or this answer

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, actually 'Blob' function is not defined in Nodejs. So I'm getting error as ReferenceError: Blob is not defined
@YashBoura Try blob for client side or node-blob for server side
I want to handle in server side. No luck with node-blob too.
@YashBoura are the firebase storage API available for server-side too? As far as I know, we have to use google client libraries to upload to firebase storage. ref
Well you are right, I have to use google cloud for this. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.