The usage of AmazonWebServices is quite simple.
- Install
AmazonWebServices
NuGet package from here.
PM> Install-Package AmazonWebServices
- Add services.AddAmazonWebServices();
builder.Services.AddAmazonWebServices();
- Add the necessary information to the
appsettings.json
file.
"AmazonCredentialOptions": {
"AccessKey": "",
"SecretKey": ""
},
"AmazonS3Options": {
"BucketName": "",
"Region": "eu-central-1"
},
"AmazonSESOptions": {
"ConfigurationSetName": "",
"SmtpOptions": {
"Host": "",
"UserName": "",
"Password": ""
}
}
- And start using it. And that's it.
To upload and delete files to Amazon S3. "UploadObjectRequest" only supports these extensions: ".jpeg", ".jpg", ".png", ".pdf", ".svg", ".webp".
[ApiController]
public class AmazonS3Service : ControllerBase
{
private readonly IAmazonS3Service _amazonS3Service;
public AmazonS3Service(IAmazonS3Service amazonS3Service)
{
_amazonS3Service = amazonS3Service;
}
[Consumes("multipart/form-data")]
[Produces("multipart/form-data", "application/json")]
[HttpPost("upload-object")]
public async Task<IActionResult> Upload([FromForm] IFormFile file, [FromQuery] string folderName, [FromQuery] string fileName)
{
var uploadedFileName = await _amazonS3Service.UploadAsync(new UploadObjectRequest
{
File = file,
FileName = fileName,
FolderName = folderName
});
return Ok(uploadedFileName);
}
[Consumes("application/json")]
[Produces("application/json", "text/plain")]
[HttpPost("upload")]
public async Task<IActionResult> Upload([FromQuery] string filePath, [FromQuery] string folderName, [FromQuery] string fileName)
{
var uploadedFileName = await _amazonS3Service.UploadAsync(new UploadRequest
{
FilePath = filePath,
FileName = fileName,
FolderName = folderName
});
return Ok(uploadedFileName);
}
[Consumes("application/json")]
[Produces("application/json", "text/plain")]
[HttpPost("delete")]
public async Task<IActionResult> Delete([FromQuery] string folderName, [FromQuery] string fileName)
{
await _amazonS3Service.DeleteAsync(fileName, folderName);
return Ok();
}
}
To send emails with Amazon SES.
[ApiController]
public class AmazonSesServiceController : ControllerBase
{
private readonly IAmazonSesService _amazonSesService;
public AmazonSesServiceController(IAmazonSesService amazonSesService)
{
_amazonSesService = amazonSesService;
}
[Consumes("application/json")]
[Produces("application/json", "text/plain")]
[HttpPost("/sendEmail")]
public async Task<IActionResult> SendEmail(SendEmailRequest sendEmailRequest)
{
await _amazonSesService.SendEMail(sendEmailRequest);
return Ok();
}
[Consumes("multipart/form-data")]
[Produces("multipart/form-data", "application/json")]
[HttpPost("/sendSmtpEmail")]
public async Task<IActionResult> SendSmtpEmail(SendSmtpEmailRequest sendSmtpEmailRequest)
{
await _amazonSesService.SendSmtpEMail(sendSmtpEmailRequest);
return Ok();
}
}