Search code examples
javarestdropwizardminio

Is it necessary to use bufferInputStream when getting a file from Minio?


I want to know if a buffer is necessary when I receive a input stream from Minio.

I use Minio as my object storage and and have Dropwizard as Backend between client and Minio. now when I use the getObject method from the minio I get a inputStream.

public InputStream getObject(String bucketName, String objectName, long offset)

In my mind it would be something like

@Path("/file")
public class FileResource {

    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getFile() throws Exception {
        InputStream is = minioClient.getObject("mybucket", "myobject");

        return Response.ok(is)
                .header(HttpHeaders.CONTENT_DISPOSITION, 
                        "attachment; filename=\"file.txt\"")
                .build();
    }
}

To my understanding it was possible to just return this input stream as response to the client with the necessary content disposition.

Now is a bufferedInputStream necesairy? And how long does the GET request wait until it is timed out?


Solution

  • I don't have access to Minio. But using a simple local file, your approach works fine.

    import com.google.common.net.HttpHeaders;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.nio.file.Paths;
    import javax.ws.rs.GET;
    import javax.ws.rs.InternalServerErrorException;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    
    @Path("/file")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public class FileResource {
    
        @GET
        public Response getFile() {
            try {
                InputStream is = new FileInputStream(Paths.get("/tmp/foo.txt").toFile());
                return Response.ok(is)
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"file.txt\"")
                        .build();
            } catch (FileNotFoundException ex) {
                throw new InternalServerErrorException(ex.getMessage());
            }
    
        }
    }
    

    A simple file /tmp/foo.txt containing some text is returned with a correct HTTP response. Using curl:

    $ curl -v http://localhost:8080/file
    *   Trying 127.0.0.1...
    * Connected to localhost (127.0.0.1) port 8080 (#0)
    > GET /file HTTP/1.1
    > Host: localhost:8080
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < Date: Wed, 11 Apr 2018 14:15:06 GMT
    < Content-Disposition: attachment; filename="file.txt"
    < Content-Type: application/octet-stream
    < Vary: Accept-Encoding
    < Content-Length: 12
    < 
    foo
    bar
    baz