从 Imgur API 收到 "Uploading file too fast!" 错误

Receiving "Uploading file too fast!" error from Imgur API

我创建了一个 node.js 服务器,它使用 busboy 接收请求,并将文件通过管道传输到 Imgur 进行上传。但是,我不断收到 Imgur 的 "Uploading file too fast!" 回复,我不确定到底是什么问题。这是涉及 busboy 的代码片段:

var express = require('express');
var Busboy = require('busboy');
var fs = require('fs');
var request = require('request-promise');
var router = express.Router();

router.post('/u', function(req, res, next) {
    var busboy = new Busboy({headers: req.headers});
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        if(fieldname == 'image') {
            var options = {
                uri: 'https://api.imgur.com/3/image',
                method: 'POST',
                headers: {
                    'Authorization': 'Client-ID ' + clientID // put client id here
                },

                form: {
                    image: file,
                    type: 'file'
                }
            };

            request(options)
                .then(function(parsedBody) {
                    console.log(parsedBody);
                })
                .catch(function(err) {
                    console.log(err);
                });
        }
    });
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
        console.log('field');
    });
    busboy.on('finish', function() {
        res.status(200).end();
    });
    req.pipe(busboy);
});

如您所见,我将请求文件直接传送到我对 imgur 的请求中。通过简单地将文件保存到光盘然后使用 fs.createReadStream() 来提供 ReadStream 效果很好,所以我不太确定为什么尝试直接从请求到请求进行管道传输会给我错误。我从 Imgur 得到的确切回复是:

StatusCodeError: 400 - {"data":{"error":"Uploading file too fast!","request":"\/3\/image","method":"POST"},"success":false,"status":400} 

如果有人以前遇到过这个,那将会很有帮助...

第一个问题是您应该使用 formData 而不是 form 来上传文件。否则,请求库将不会发送正确的 HTTP 请求。

第二个问题是流对象在完全处理之前不会有正确的内容长度。我们可以自己缓冲数据,并在处理完来自 busboy 的初始文件流后将其传递。*

这给了我们一些看起来像

的东西
var express = require('express');
var Busboy = require('busboy');
var fs = require('fs');
var request = require('request-promise');
var router = express.Router();

router.post('/u', function(req, res, next) {
    var busboy = new Busboy({headers: req.headers});
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        if(fieldname == 'image') {
            // the buffer
            file.fileRead = [];
            file.on('data', function(data) {
                // add to the buffer as data comes in
                this.fileRead.push(data);
            });

            file.on('end', function() {
                // create a new stream with our buffered data
                var finalBuffer = Buffer.concat(this.fileRead);

                var options = {
                    uri: 'https://api.imgur.com/3/image',
                    method: 'POST',
                    headers: {
                        'Authorization': 'Client-ID ' + clientID // put client id here
                    },

                    formData: {
                        image: finalBuffer,
                        type: 'file'
                    }
                };

                request(options)
                    .then(function(parsedBody) {
                        console.log(parsedBody);
                    })
                    .catch(function(err) {
                        console.log(err);
                    });
            });
        }
    });
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
        console.log('field');
    });
    busboy.on('finish', function() {
        res.status(200).end();
    });
    req.pipe(busboy);
});

最后,您可能需要考虑使用请求库,因为请求承诺库不鼓励使用流。有关详细信息,请参阅 github 存储库:https://github.com/request/request-promise