gpt4 book ai didi

java - 输入和输出流的 IOUtils.copy() 速度非常慢

转载 作者:行者123 更新时间:2023-12-02 01:48:05 25 4
gpt4 key购买 nike

作为我的 Web 服务的一部分,我有一个图片存储库,它从 Amazon S3(数据存储)检索图像然后返回它。执行此操作的方法如下所示:

File getPicture(String path) throws IOException {
File file = File.createTempFile(path, ".png");
S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, path));
IOUtils.copy(object.getObjectContent(), new FileOutputStream(file));
return file;
}

问题是从服务获得响应需要很长时间 - (下载 3MB 的图像需要 7.5 秒)。我注意到,如果我注释掉 IOUtils.copy() 行,响应时间会明显加快,因此一定是特定方法导致了这种延迟。

我在几乎所有将 S3Object 转换为文件的现代示例中都看到过这种方法,但我似乎是一个独特的案例。我在这里错过了一个技巧吗?

感谢任何帮助!

最佳答案

来自 AWS 文档:

public S3Object getObject(GetObjectRequest getObjectRequest)

the returned Amazon S3 object contains a direct stream of data from the HTTP connection. The underlying HTTP connection cannot be reused until the user finishes reading the data and closes the stream.

public S3ObjectInputStream getObjectContent()

Note: The method is a simple getter and does not actually create a stream. If you retrieve an S3Object, you should close this input stream as soon as possible, because the object contents aren't buffered in memory and stream directly from Amazon S3.

<小时/>

如果删除 IOUtils.copy 行,则方法会快速退出,因为您实际上并未处理流。如果文件很大,则下载需要时间。除非您能够更好地连接到 AWS 服务,否则您对此无能为力。

关于java - 输入和输出流的 IOUtils.copy() 速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53439403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com