diff --git a/Worker/src/Common/Adapter/S3Adapter.cs b/Worker/src/Common/Adapter/S3Adapter.cs
index f0a0f419..dc42b7d9 100644
--- a/Worker/src/Common/Adapter/S3Adapter.cs
+++ b/Worker/src/Common/Adapter/S3Adapter.cs
@@ -16,12 +16,14 @@
using System;
using System.IO;
+using System.Threading;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
using ArmoniK.DevelopmentKit.Common;
+using ArmoniK.Utils;
namespace ArmoniK.DevelopmentKit.Worker.Common.Adapter;
@@ -83,48 +85,42 @@ public S3Adapter(string endPointRegion,
/// The filename with extension and without directory path
/// Returns the path where the file has been downloaded
public string DownloadFile(string fileName)
- {
- DownloadFileAsync(fileName)
- .Wait();
-
- return Path.Combine(DestinationDirPath,
- fileName);
- }
+ => DownloadFileAsync(fileName)
+ .WaitSync();
-
- public async Task DownloadFileAsync(string fileName)
+ ///
+ /// The method to download file from form remote server
+ ///
+ /// The filename with extension and without directory path
+ /// Abort download of the file
+ /// Returns the path where the file has been downloaded
+ public async Task DownloadFileAsync(string fileName,
+ CancellationToken cancellationToken = default)
{
- var ms = new MemoryStream();
-
var r = await Client.GetObjectAsync(new GetObjectRequest
{
BucketName = BucketName,
Key = fileName,
- });
- var stream2 = new BufferedStream(r.ResponseStream);
+ },
+ cancellationToken);
- var materializedFileName = fileName + Guid.NewGuid();
+ var materializedFileName = Path.Combine(LocalZipDir,
+ fileName + Guid.NewGuid());
+
+ var targetFileName = Path.Combine(DestinationDirPath,
+ fileName);
- var file = new FileStream(Path.Combine(LocalZipDir,
- materializedFileName),
- FileMode.Create,
- FileAccess.Write);
try
{
- var buffer = new byte[0x2000];
- var count = 0;
- while ((count = stream2.Read(buffer,
- 0,
- buffer.Length)) > 0)
- {
- ms.Write(buffer,
- 0,
- count);
- }
+ await r.WriteResponseStreamToFileAsync(materializedFileName,
+ false,
+ cancellationToken)
+ .ConfigureAwait(false);
- ms.WriteTo(file);
- file.Close();
- ms.Close();
+ File.Move(materializedFileName,
+ targetFileName);
+
+ return targetFileName;
}
catch (AmazonS3Exception amazonS3Exception)
{
@@ -135,10 +131,12 @@ public async Task DownloadFileAsync(string fileName)
throw new Exception("Error occurred: " + amazonS3Exception.Message);
}
-
- File.Move(Path.Combine(LocalZipDir,
- materializedFileName),
- Path.Combine(LocalZipDir,
- fileName));
+ finally
+ {
+ if (File.Exists(materializedFileName))
+ {
+ File.Delete(materializedFileName);
+ }
+ }
}
}