Express 快速获取图片并发送至imgur

Express 快速获取图片并发送至imgur,express,request,axios,multipart,body-parser,Express,Request,Axios,Multipart,Body Parser,我用的是带nodejs的express, 我想用imgur来主持图片。 我有我的访问令牌,我需要请求此端点:“ 标题:headers:{'Authorization':“bearer”+config.access\u token\u imgur,'content type':'multipart/form data'}带有给定图片 要做到这一点,第一个问题是处理图片:这是多部分的,而bodyparser不能。 我试过multer,但它总是保存图片,我只想得到它,并将其发布到端点,所以, 我的目标

我用的是带nodejs的express, 我想用imgur来主持图片。 我有我的访问令牌,我需要请求此端点:“ 标题:
headers:{'Authorization':“bearer”+config.access\u token\u imgur,'content type':'multipart/form data'}
带有给定图片

要做到这一点,第一个问题是处理图片:这是多部分的,而bodyparser不能。 我试过multer,但它总是保存图片,我只想得到它,并将其发布到端点,所以, 我的目标是:

    router.post("/upload",upload.single('file'), async (req,res)=>{ 
  if(req.file){
    var headers = {
        headers: {"Authorization" : `Bearer ${config.access_token_imgur}`, 'content-type': 'multipart/form-data'}
    };
    var formData = new FormData();
    //formData.append("image", req.file);
    axios.post('https://api.imgur.com/3/upload', formData, headers).then((res)=>{
        console.log(res);
        console.log(res.body);
        console.log(res.data);
    }).catch((error)=>{
        console.log(error);
    })

    //must get a picture from the parameters (how to handle ? )
    //send it to https://api.imgur.com/3/upload with headers with post method
    //handle response
  }else{
      res.status(500).json({success: false})
  }
})

正如你所看到的,我尝试了multer,但我认为这不是一个好的答案: 如何处理图片而不保存?(多部分),并将其发送到端点?(axios,请求,…)
谢谢

您是正确的,bodyParser不会帮助您处理多部分数据

解决方案1: 您仍然可以使用multer进行处理,但使用不同的存储引擎。例如,您可以使用它来缓存正在内存中处理的文件,而不实际将它们写入磁盘

解决方案2: 如果您想跳过图像处理,可以使用以下代码将请求转发给第三方服务:

const request = require('request')
...

router.post('/upload', (req,res, next) => { 
  const forwardReqConfig = {
    url: 'https://api.imgur.com/3/upload',
    headers: {
      'Authorization': `bearer ${config.access_token_imgur}`,
      'Content-Type': 'multipart/form-data'
    }
  };
  req.pipe(request.post(forwardReqConfig)).pipe(res)
})

由于express对象继承自nodejs对象,所以它也是一个流。这同样适用于res对象。您可以通过管道将req流传输到由创建的流中,然后通过管道将来自第三方服务的响应返回到API的res中

我希望这能有所帮助。

我做到了:

 router.post('/upload', (req, res, next)=> { //https://stackoverflow.com/questions/35534589/receiving-uploading-file-too-fast-error-from-imgur-api --> must verify file type
    var busboy = new Busboy({ 
      headers: req.headers,
      limits: {
        fileSize: 1000000 * 200, //limit to 200mb because imgur's limit, and 1gb is buffer's limit --> must pass on aws s3 if needed - limit in byte
        files: 1,
      }
    });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        if(fieldname == 'image') {
            // the buffer
            file.fileRead = []; //------------need convert to gif !!----------- ------------------------------------------------
            file.on('data', function(data) {
                // add to the buffer as data comes in
                this.fileRead.push(data);
            });
            file.on('limit', ()=>{
              res.status(500).json({success: false, message: "file too big ! Musn't cross the line of 200mo ! We'll soon reduce this limit"})
            })
            file.on('end', function() {
                // create a buffer
                var finalBuffer = Buffer.concat(this.fileRead); 
                upload = uploadpicture(finalBuffer, mimetype).then((response)=>{ //success request
                  console.log(response);
                  res.status(200).json({success: true, message: "Successfully uploaded !", url: response.data.link});
                },(err)=>{ //error
                  console.log(err);
                  res.status(500).json({success: false, message: "Error happenned while uploading !"});
                }).catch((err)=>{
                  console.log(err);
                  res.status(500).json({success: false, message: "Error happenned while uploading !"});
                });
            })
        }
    });
/*    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
        console.log('field');
    });*/
    busboy.on('finish', function() {
        //busboy finished
    });
    req.pipe(busboy);
}); 

您好,非常感谢您的回答;)我正在尝试使用您的代码,但在工作时,我收到状态为403的“格式错误的验证头”。所以我试着把b的“承载者”改成“承载者”,我得到了400的状态,没有回应体,知道吗?谢谢^^根据
承载人的说法
似乎是imgur接受的承载人。对你来说,我想可能会有帮助。
/3/upload
端点需要客户端ID而不是承载令牌,因此您可以尝试使用它。