Enforce file upload size on Heroku?

2019-02-25 00:39发布

问题:

Got a Rails app on Heroku. I want to limit the size of a file upload if possible. The file is processed as a StringIO object, so the file contents will be processed in memory (have no need to write an intermediate file to a filesystem).

Ordinarily, I would limit upload size on the web server. But with Heroku, what options are there? I realize I could go with a Flash uploader, but I'm hoping to avoid requiring Flash on the client, if at all possible.

回答1:

Heroku limits the request entity to some small size (30MB), and also limits all request/response cycles to 30s. Both are hard rules and must be lived with.

Rails by default spools file uploads to disk and translates them to Tempfile instances before handing them off to your application.



回答2:

You could run NGINX in front of your app server to enforce this constraint.

This is fairly simple with heroku, you will need a nginx.conf and this buildpack.

# nginx.conf
http {
    #...
    client_max_body_size 100m;
    #...
}

Note: But be aware that on heroku, NGINX run inside your dynos (ie. local to each instance).

https://blog.codeship.com/how-to-deploy-nginx-on-heroku

Hope that's help!