|
29 | 29 | import java.util.Date; |
30 | 30 | import java.util.List; |
31 | 31 |
|
32 | | - |
33 | 32 | public class ExternalProviderS3V1 extends ExternalProviderBase implements ExternalProvider { |
34 | 33 | private static Logger logger = LogManager.getLogger(ExternalProviderS3V1.class); |
35 | 34 |
|
@@ -173,21 +172,31 @@ private String ensureFolder(String... pathPart) { |
173 | 172 | } |
174 | 173 |
|
175 | 174 | public void download(String externalFileName, String localFile, ResourceAccessControlList acl) { |
| 175 | + S3ObjectInputStream objectData = null; |
176 | 176 | try { |
177 | 177 | 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); |
185 | 189 | } |
186 | 190 | } |
187 | 191 | } catch (FileNotFoundException ex) { |
188 | 192 | logger.error("Error while downloading file to the external provider", ex); |
189 | 193 | } catch (IOException ex) { |
190 | 194 | 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);} |
191 | 200 | } |
192 | 201 | } |
193 | 202 |
|
|
0 commit comments