Skip to content

Commit b614c63

Browse files
Drain entire download stream (#830)
* Drain entire download stream Issue: 107019 * Close objectData after downloading resource * Correctly close objectData * Remove unncesary catch * Change the position of the finally block
1 parent 0106855 commit b614c63

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

gxcloudstorage-awss3-v1/src/main/java/com/genexus/db/driver/ExternalProviderS3V1.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.Date;
3030
import java.util.List;
3131

32-
3332
public class ExternalProviderS3V1 extends ExternalProviderBase implements ExternalProvider {
3433
private static Logger logger = LogManager.getLogger(ExternalProviderS3V1.class);
3534

@@ -173,21 +172,31 @@ private String ensureFolder(String... pathPart) {
173172
}
174173

175174
public void download(String externalFileName, String localFile, ResourceAccessControlList acl) {
175+
S3ObjectInputStream objectData = null;
176176
try {
177177
S3Object object = client.getObject(new GetObjectRequest(bucket, externalFileName));
178-
try (InputStream objectData = object.getObjectContent()) {
179-
try (OutputStream outputStream = new FileOutputStream(new File(localFile))){
180-
int read;
181-
byte[] bytes = new byte[1024];
182-
while ((read = objectData.read(bytes)) != -1) {
183-
outputStream.write(bytes, 0, read);
184-
}
178+
objectData = object.getObjectContent();
179+
File file = new File(localFile);
180+
File parentDir = file.getParentFile();
181+
if (parentDir != null && !parentDir.exists())
182+
parentDir.mkdirs();
183+
184+
try (OutputStream outputStream = new FileOutputStream(file)) {
185+
int read;
186+
byte[] bytes = new byte[1024];
187+
while ((read = objectData.read(bytes)) != -1) {
188+
outputStream.write(bytes, 0, read);
185189
}
186190
}
187191
} catch (FileNotFoundException ex) {
188192
logger.error("Error while downloading file to the external provider", ex);
189193
} catch (IOException ex) {
190194
logger.error("Error while downloading file to the external provider", ex);
195+
} finally {
196+
try {
197+
if (objectData != null)
198+
objectData.close();
199+
} catch (IOException ioe) {logger.error("Error while draining the S3ObjectInputStream", ioe);}
191200
}
192201
}
193202

0 commit comments

Comments
 (0)