How to handle large file uploads in Quarkus

Viewed 7566

My question is two fold:

  1. I'm looking for a way to implement multipart file uploads on a quarkus application server that allows the programmer access to the uploaded file. Going through the documentation it seems there is https://quarkus.io/guides/rest-client-multipart, but this doesn't tell me where on the server, the file is stored (if it is at all).

  2. I'd also like to know what's going on under the hood. For large files (>100MB) what would the memory footprint look like?

5 Answers

You should use quarkus.http.limits.max-body-size, I tried and success with 200M

Quarkus uses the combination of the following properties to manage multipart payloads:

  1. quarkus.http.body.handle-file-uploads=true to determine whether or not to accept multipart requests. According to the docs though, your app will still accept multipart requests regardless of what this is set to.

  2. quarkus.http.body.uploads-directory the relative or absolute path where the uploads should be held

  3. quarkus.http.body.delete-uploaded-files-on-end to control whether uploaded files are cleared at the end of the request.

  4. quarkus.http.body.preallocate-body-buffer =true to set a buffer size to hold the request body, based on the number in Content-Length HTTP header.

I'd also like to know what's going on under the hood. For large files (>100MB) what would the memory footprint look like?

That's an open-ended question that no one can safely prognosticate. You won't know until you try. Your deployment mode (native or JVM) also matters - native mode does serial garbage collection and can cause longer waits while GC happens.

As @kolossus has pointed out, You can configure file handling through the built-in quarkus options. However, it seems that quarkus will handle file uploads in pretty much the exact same way regardless of whether or not these options are set.

For example, using RESTEasy's @MultipartForm

public class Resource {

    @FormParam("file")
    public File file;
}

And then in some controller method:

@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response fileUpload(@MultipartForm Resource upload) {
    log.info("File path: {}", upload.file.getAbsolutePath());
}

Then call this method with a file in the request body and you should see the log output containing the path to the uploaded file (Something ugly like /var/folders/y_/_3mfhd2s5ks08fsmhnn80q440000gn/T/5e84410f27304d289f168a76d51b240a).

When you look in that path however, you wont see that file anywhere.

So it seems that quarkus has the options set to the corresponding values by default:

quarkus.http.body.handle-file-uploads=true

quarkus.http.body.uploads-directory=<some random directory in /var>

quarkus.http.body.delete-uploaded-files-on-end=true

Note: tested on v1.4.1.Final

I had the same problem and none of the old answers helped me. My endpoint was receiving a @MultipartForm object, which defines the parameter of the endpoint to receive a multipart/form-data mime type object.

After digging the Quarkus documentation, the property I really need was this:

quarkus.http.limits.max-form-attribute-size

I have set this to 4M, since this was the limit that I was already using the code, like this, on my application.yaml file:

quarkus:
    http:
        limits:
            max-form-attribute-size: 4M

Keep in mind that if you are using an application.properties file instead, you should do it like this:

quarkus.http.limits.max-form-attribute-size=4M

Quarkus uses the RESTEasy implementation from JBoss. So, if you look at their documentation, you'll find what you need.

Related