anacronw / multer-s3

multer storage engine for amazon s3
MIT License
660 stars 190 forks source link

Upload large files net :: ERR_EMPTY_RESPONSE #137

Closed iscorporacion closed 4 years ago

iscorporacion commented 4 years ago

I am having problems with this library and you cannot imagine how much information with the same error can be found on the Internet, you will see.

I am building an app to upload files from 150mb to S3 but multer-s3 only allows uploading very small files, in fact doing tests I realize that it really does upload after a while, but the connection with the client is closed after 2 minutes, I have tried everything and I think that on loading the file something closes the connection giving as error net :: ERR_EMPTY_RESPONSE, try to modify req.setTimeout and res.setTimeout without success and as I mention after a while I go to the S3 console And the charge file, some could tell me what may be happening, vis several related cases but none of them talks about how it was solved. this is my code, thank you all very much

----- CLIENT -----

const data = new FormData();
data.append('profileImage', this.state.selectedFile, this.state.selectedFile.name);
let client = new XMLHttpRequest()
client.open("POST", "/api/profile/profile-img-upload")
client.timeout = 600000;
client.send(data)

----- SERVER-----

const express = require('express');
const cors = require('cors');
const path = require('path');

const app = express();

app.use(cors({
    "origin": "*",
    "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
    "preflightContinue": false,
    "optionsSuccessStatus": 204
}));
app.use(express.json({ limit: '150mb' }));
app.use(express.urlencoded({ limit: '150mb', extended: true }));

const profile = require('./routes/api/profile');
app.use('/api/profile', profile);

const port = process.env.PORT || 5000;
const server = require('http').Server(app);
server.listen(port, () => console.log(`Server running on port: ${port}`));
server.timeout = 600000;

----- ROUTER -----

function extendTimeout(req, res, next) {
    res.setTimeout(600000, function (){ 
    })
    req.setTimeout(600000, function (){ 
    })
    next();
}
const s3 = new aws.S3({
    accessKeyId: ',
    secretAccessKey: '',
    region: '',
});
var limits = {
    files: 1,
    fileSize: 150 * 1024 * 1024,
};
var upload = multer({
    storage: multerS3({
        limits: limits,
        s3: s3,
        bucket: 'esan-files',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        contentLength: limits.fileSize,
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname });
        },
        key: function (req, file, cb) {
            cb(null, file.originalname);
        }
    })
}).single('profileImage');

router.post('/profile-img-upload', extendTimeout, function (req, res) {
    await upload(req, res, async (err) => {
        res.send({ "message": 'Successfully uploaded ' });
    });
})

I have seen a lot of information on the Internet and all the questions, I think that multer-s3 needs a different approach than how they teach how to use it, they limit themselves to teaching only the use and not to the integration with the server either native node or express, I think that There should be a complete example of usage and it really works. I am sure many would pay for this support.

MrScX commented 4 years ago

Are you behind NGINX? If so, try setting client_max_body_size in nginx.conf. It's probably that issue.

iscorporacion commented 4 years ago

No lo he subido a un servidor lo pruebo en local, y el tiempo de la petición se agota, tengo ganas de montar un servidor local de nginx haber que pasa, pero no creo que sea client_max_body_size, si hay una forma de configurar esto desde node en node, sin crear un servidor agradecería me indicaras

I have not uploaded it to a server I try it locally, and the request time is running out, I want to mount a local nginx server if it happens, but I don't think it is client_max_body_size, if there is a way to configure this from node in node, without creating a server I would appreciate you indicate

iscorporacion commented 4 years ago

Bueno para todos, los que estan teniendo el problema

Are you behind NGINX? If so, try setting client_max_body_size in nginx.conf. It's probably that issue.

For those who are getting this error, after a long time I can say that apparently as commented in another comment it has to do with the server, for the library to work you have to configure some sections of the server, but as we usually do tests locally node does not have these settings, for it to work you must put your app behind a web server, I used NGINX, configuring several things, in the global

configuration client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
types_hash_max_size 2048;

client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 250m;
 large_client_header_buffers 2 1k;

and on the site

proxy_read_timeout sites 15m; 
proxy_connect_timeout 15m;

with this it no longer generates that error, I hope it can serve whoever has these problems, it is not a problem with the library but the server,