【问题标题】:Node.js - How do I upload an image to Backblaze b2?Node.js - 如何将图像上传到 Backblaze b2?
【发布时间】:2021-12-10 01:44:21
【问题描述】:

我正在从 b2.uploadFIle() 连接 Request failed with status code 400

这是我尝试做的:

第一步:下载backblaze-b2 node.js library multer(to get image file in req.body)

第 2 步:设置在 POSTMAN 中调用路线的方式。我在请求中附上了 IronMan.png。

第 3 步:设置我的代码:

       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: "bucketName",
        });

        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.
            });
        });

我使用 multer 从表单请求中获取图像文件,然后将其作为缓冲区传递给 b2.uploadFile 的数据属性。

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript node.js image multer


    【解决方案1】:

    已修复!我所要做的就是去掉 b2.uploadFile() 中的可选参数

    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);
                }
              
                b2.getUploadUrl({
                    bucketId: process.env.backblazeb2BucketId,
                    // ...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",
                        // contentLength: 0, // optional data length, will default to data.byteLength or data.length if not provided
                        //mime: "", // optional mime type, will default to 'b2/x-auto' if not provided
                        data: req.file.buffer, // this is expecting a Buffer, not an encoded string
                        //hash: "sha1-hash", // optional data hash, will use sha1(data) if not provided
                        // info: {
                        //     // optional info headers, prepended with X-Bz-Info- when sent, throws error if more than 10 keys set
                        //     // valid characters should be a-z, A-Z and '-', all other characters will cause an error to be thrown
                        //     key1: "value",
                        //     key2: "value",
                        // },
                        onUploadProgress: (event) => {},
                        //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.
                });
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      相关资源
      最近更新 更多