简体   繁体   English

具有输入和输出流的IOUtils.copy()非常慢

[英]IOUtils.copy() with input and output streams is extremely slow

As part of my web service, I have a picture repository which retrieves an image from Amazon S3 (a datastore) then returns it. 作为我的Web服务的一部分,我有一个图片存储库,该存储库从Amazon S3(数据存储)中检索图像,然后将其返回。 This is how the method that does this looks: 这是执行此操作的方法的外观:

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

The problem is that it takes way too long to get a response from the service - (a 3MB image took 7.5 seconds to download). 问题是从服务获得响应花费的时间太长-(一个3MB的图像花了7.5秒下载时间)。 I notice that if I comment out the IOUtils.copy() line, the response time is significantly faster so it must be that particular method that's causing this delay. 我注意到,如果我注释掉IOUtils.copy()行,则响应时间明显加快,因此,一定是导致这种延迟的特定方法。

I've seen this method used in almost all modern examples of converting an S3Object to a file but I seem to be a unique case. 我已经在几乎所有将S3Object转换为文件的现代示例中都使用了这种方法,但是我似乎是一个特例。 Am I missing a trick here? 我在这里错过了一个把戏吗?

Appreciate any help! 感谢任何帮助!

From the AWS documentation: 从AWS文档中:

public S3Object getObject(GetObjectRequest getObjectRequest)

the returned Amazon S3 object contains a direct stream of data from the HTTP connection. 返回的Amazon S3对象包含来自HTTP连接的直接数据流。 The underlying HTTP connection cannot be reused until the user finishes reading the data and closes the stream. 在用户完成读取数据并关闭流之前,基础HTTP连接无法重用。

public S3ObjectInputStream getObjectContent()

Note: The method is a simple getter and does not actually create a stream. 注意:该方法是一个简单的getter,实际上并不创建流。 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. 如果您检索S3Object,则应尽快关闭此输入流,因为对象内容不会在内存中缓冲,而不会直接从Amazon S3流。


If you remove the IOUtils.copy line, then method exits quickly because you don't actually process the stream. 如果删除IOUtils.copy行,则方法会快速退出,因为您实际上并未处理该流。 If the file is large it will take time to download. 如果文件很大,将需要一些时间来下载。 You can't do much about that unless you can get a better connection to the AWS services. 除非您可以更好地连接到AWS服务,否则您将无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM