RED-6864 - Optimization of Upload and Download for Large Files in Azure Blob Storage and AWS S3/MinIO

- close streams when done
This commit is contained in:
Corina Olariu 2023-08-11 13:20:40 +03:00
parent efad7fa66a
commit 974a959fdd

View File

@ -4,6 +4,10 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.UUID;
@ -45,7 +49,10 @@ public class ReportStorageService {
public byte[] getReportTemplate(String storageId) {
try {
return IOUtils.toByteArray(getObject(TenantContext.getTenantId(), storageId));
InputStream inputStream = getObject(TenantContext.getTenantId(), storageId);
byte[] storedObjectBytes = IOUtils.toByteArray(inputStream);
inputStream.close();
return storedObjectBytes;
} catch (IOException e) {
throw new RuntimeException("Could not get report template.");
}
@ -55,7 +62,11 @@ public class ReportStorageService {
@SneakyThrows
public byte[] getStoredObjectBytes(String dossierId, String fileId, FileType fileType) {
return IOUtils.toByteArray(getObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, fileType)));
InputStream inputStream = getObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, fileType));
byte[] storedObjectBytes = IOUtils.toByteArray(inputStream);
inputStream.close();
return storedObjectBytes;
}
@ -65,11 +76,10 @@ public class ReportStorageService {
}
@SneakyThrows
public FileInputStream getObject(String tenantId, String storageId) {
public InputStream getObject(String tenantId, String storageId) {
File destFile = File.createTempFile("destFile", ".data");
destFile.deleteOnExit();
storageService.downloadTo(tenantId, storageId, destFile);
return new FileInputStream(destFile);
return Files.newInputStream(Paths.get(destFile.getPath()), StandardOpenOption.DELETE_ON_CLOSE);
}
}