Skip to content

Commit

Permalink
fix: Avoid materializing S3 lib in memory (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo authored Aug 9, 2024
2 parents 9bda4b0 + 552d016 commit 646a267
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions Worker/src/Common/Adapter/S3Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,48 +85,42 @@ public S3Adapter(string endPointRegion,
/// <param name="fileName">The filename with extension and without directory path</param>
/// <returns>Returns the path where the file has been downloaded</returns>
public string DownloadFile(string fileName)
{
DownloadFileAsync(fileName)
.Wait();

return Path.Combine(DestinationDirPath,
fileName);
}
=> DownloadFileAsync(fileName)
.WaitSync();


public async Task DownloadFileAsync(string fileName)
/// <summary>
/// The method to download file from form remote server
/// </summary>
/// <param name="fileName">The filename with extension and without directory path</param>
/// <param name="cancellationToken">Abort download of the file</param>
/// <returns>Returns the path where the file has been downloaded</returns>
public async Task<string> 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)
{
Expand All @@ -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);
}
}
}
}

0 comments on commit 646a267

Please sign in to comment.