Node.js - How do I upload an image to Backblaze b2?

I am getting connect Request failed with status code 400 from b2.uploadFIle()

Here's what I tried to do:

Step 1: Download backblaze-b2 node.js library and multer(to get image file in req.body)

Step 2: Set up how I'm going to call my route in POSTMAN. I have attached an IronMan.png in my request. enter image description here

Step 3: Set up my code:

       import B2 from "backblaze-b2";  
  export const uploadCreationImage = async (
) => {
    try {
        const b2 = new B2({
            applicationKeyId: process.env.backblazeb2ApplicationKeyId, 
            applicationKey: process.env.backblazeb2ApplicationKey, 
        });

        await b2.authorize(); // must authorize first (authorization lasts 24 hrs)
        console.log("I am here");
        let response = await b2.getBucket({
            bucketName: "bobbyhill",
        });

        var storage = multer.diskStorage({
            destination: function (req, file, cb) {
                cb(null, "./uploads");
            },
            filename: function (req, file, cb) {
                cb(null, file.originalname);
            },
        });

        const multerUploader = multer({});
 upload(req, res, (err: any) => {
            if (err instanceof multer.MulterError) {
                return res.sendStatus(INTERNAL_SERVER_ERROR_STATUS);
                // A Multer error occurred when uploading.
            } else if (err) {
                // An unknown error occurred when uploading.
                return res.sendStatus(INTERNAL_SERVER_ERROR_STATUS);
            }
            // console.log("joe", req.file.buffer);
            // console.log("biden", req.file);
            b2.getUploadUrl({
                bucketId: "58dd09e54842aafc7dcd0917",
                // ...common arguments (optional)
            }).then((response) => {
                console.log("getUploadUrl", response.data.uploadUrl , response.data.authorizationToken);
                
                b2.uploadFile({
                    uploadUrl: response.data.uploadUrl,
                    uploadAuthToken: response.data.authorizationToken,
                    fileName: "fileName",
                    data: req.file.buffer, // this is expecting a Buffer, not an encoded string
             
                
                    onUploadProgress: null,
                    //onUploadProgress: (event) => {} || null // progress monitoring
                    // ...common arguments (optional)
                }).then((response) => {
                    console.log('uploadFIle', response); 
                    return res.send({ path: req.file.originalname });
                } 
               
                // Everything went fine and save document in DB here.
            });
        });

I used multer to get the image file from the form request and then pass it to b2.uploadFile's data property as a buffer.

Any help would be appreciated!



from Recent Questions - Stack Overflow https://ift.tt/3C915wg
https://ift.tt/3BeA8pE

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)