从Minio获取文件时是否有必要使用bufferInputStream



我想知道当我从Minio接收输入流时是否需要缓冲区。

我使用Minio作为我的对象存储,并将Dropwizard作为客户端和Minio之间的后端。 现在,当我使用minio的getObject方法时,我得到了一个inputStream。

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

在我看来,它会是这样的

@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();
    }
}

据我了解,可以仅将此输入流作为对具有必要内容处置的客户端的响应返回。

现在是否需要缓冲输入流?GET 请求要等多久才能超时?

我无法访问Minio。但是使用简单的本地文件,您的方法可以正常工作。

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());
        }
    }
}

将返回包含一些文本的简单文件/tmp/foo.txt,并带有正确的 HTTP 响应。使用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

相关内容

  • 没有找到相关文章

最新更新