Skip to content

Commit

Permalink
Merge pull request #158 from careerfairsystems/cleaningAWSfiles
Browse files Browse the repository at this point in the history
Rensat lite i Aws3Services
  • Loading branch information
LeoFjatstrom authored May 14, 2024
2 parents a627d72 + 5d8948f commit 7e4e05c
Showing 1 changed file with 28 additions and 49 deletions.
77 changes: 28 additions & 49 deletions Nexpo/AWS/Aws3Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,52 @@ public Aws3Services(string awsAccessKeyId, string awsSecretAccessKey, string reg
/// <param name="name">The name of the file</param>
public async Task<bool> UploadFileAsync(IFormFile file, string name)
{
try
using (var newMemoryStream = new MemoryStream())
{
using (var newMemoryStream = new MemoryStream())
file.CopyTo(newMemoryStream);
var uploadRequest = new TransferUtilityUploadRequest
{
file.CopyTo(newMemoryStream);
var uploadRequest = new TransferUtilityUploadRequest
{
InputStream = newMemoryStream,
Key = name,
BucketName = _bucketName,
ContentType = file.ContentType
};
InputStream = newMemoryStream,
Key = name,
BucketName = _bucketName,
ContentType = file.ContentType
};

var fileTransferUtility = new TransferUtility(_awsS3Client);
await fileTransferUtility.UploadAsync(uploadRequest);
return true;
}
}
catch (Exception)
{
throw;
var fileTransferUtility = new TransferUtility(_awsS3Client);
await fileTransferUtility.UploadAsync(uploadRequest);
return true;
}
}


/// <summary>
/// Downloads a file from AWS S3
/// </summary>
/// <param name="file">The name of the file to download</param>
public async Task<byte[]> DownloadFileAsync(string file)
{
MemoryStream ms = null;
try
MemoryStream ms = null;

GetObjectRequest getObjectRequest = new GetObjectRequest
{
GetObjectRequest getObjectRequest = new GetObjectRequest
{
BucketName = _bucketName,
Key = file
};
BucketName = _bucketName,
Key = file
};

using (var response = await _awsS3Client.GetObjectAsync(getObjectRequest))
using (var response = await _awsS3Client.GetObjectAsync(getObjectRequest))
{
if (response.HttpStatusCode == HttpStatusCode.OK)
{
if (response.HttpStatusCode == HttpStatusCode.OK)
using (ms = new MemoryStream())
{
using (ms = new MemoryStream())
{
await response.ResponseStream.CopyToAsync(ms);
}
await response.ResponseStream.CopyToAsync(ms);
}
}

if (ms is null || ms.ToArray().Length < 1)
throw new FileNotFoundException(string.Format("The document '{0}' is not found", file));
return ms.ToArray();
}
catch (Exception)
{
throw;
}

if (ms is null || ms.ToArray().Length < 1)
throw new FileNotFoundException(string.Format("The document '{0}' is not found", file));
return ms.ToArray();
}

/// <summary>
Expand Down Expand Up @@ -116,21 +104,12 @@ public bool IfFileExists(string fileName)
};

var response = _awsS3Client.GetObjectMetadataAsync(request).Result;

return true;
}
catch (Exception ex)
{
if (ex.InnerException != null && ex.InnerException is AmazonS3Exception awsEx)
{
if (string.Equals(awsEx.ErrorCode, "NoSuchBucket"))
return false;
return ex.InnerException != null && ex.InnerException is AmazonS3Exception awsEx && (string.Equals(awsEx.ErrorCode, "NoSuchBucket") || string.Equals(awsEx.ErrorCode, "NotFound"));

else if (string.Equals(awsEx.ErrorCode, "NotFound"))
return false;
}
return false;
throw;
}
}
}
Expand Down

0 comments on commit 7e4e05c

Please sign in to comment.