首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js -如何将图片上传到Backblaze b2?

Node.js -如何将图片上传到Backblaze b2?
EN

Stack Overflow用户
提问于 2021-10-24 02:08:29
回答 1查看 140关注 0票数 1

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

这是我想要做的:

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

步骤2:在POSTMAN中设置如何调用我的路由。我已在请求中附加了IronMan.png。

步骤3:设置我的代码:

代码语言:javascript
复制
       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的data属性。

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-24 23:32:46

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

代码语言:javascript
复制
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.
            });
        });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69693383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档