Hello .NET Developers!
Starting the new topic here, today we will explore the method of easily downloading file from amazon s3 bucket using .net web api. I have divided this post into two segments.
- Generating an access key to enable local machine access to an Amazon S3 (note – taking into account that you now already haveS3 bucket alongside file in it.)
- Developing the .net web api project to retrieve files from an S3 bucket
Let’s jump right in without wasting more time.
- Generating an access key to enable local machine access to an Amazon S3
I am considering that you already have s3 bucket in your aws account so now you need to create an access key to access that bucket data and here is how to do it.
- Log in to the AWS Management Console with your account. Navigate to your account, and then click on the Security Credentials option.
- On the left side panel, select users and the click on the Create User button.
- Now give username and then click on next
- At this moment, on set permissions screen, you should select the attach policies directly alternative.
- In permissions policies section search for S3 and select the policy entitled AmazonS3FullAccess.
- Here we have successfully created user by clicking on Create User after clicking Next.
- Next, navigate to the users section located on the left-hand side menu, then select the user you previously created and finally click on the option to create access key.
- On Access key best practices & alternatives select option local code, check confirmation box and click next.
- On the next screen click on create access key
- Now that you can see it, the access key with secret access key has been created. Remember to write down both keys because we will use them later, or you can also download a .csv
2. .Net API project.
Now moving to the second segment of this blog, let’s develop an application programming interface (API) for downloading a file from an Amazon S3 bucket.
Launch Visual Studio or VS Code, and create an ASP.NET Core Web API project within your Integrated Development Environment (IDE).
- Feels free to delete WeatherForecast along with WeatherForecastController because they are not necessary for us anymore.
- Install nuget packages AWSSDK.Core & AWSSDK.S3 to your project
- Now, configuration details should be added in appsetting.json; please insert the following code in your appsettings.json file.
"AWS": {
"AccessKey": "copy paste your accesskey",
"SecretKey": "copy pasteyour secretkey",
"Region": "your bucket rigion", // eu-north-1
"BucketName": "your bucket name"
}
- This is a how your appsettings.json file will look like.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AWS": {
"AccessKey": "copy paste your accesskey",
"SecretKey": "copy pasteyour secretkey",
"Region": "your bucket rigion", // eu-north-1
"BucketName": "your bucket name"
},
"AllowedHosts": "*"
}
- Now in your program.cs file add following code
// Configure AWS S3 client
var awsOptions = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(builder.Configuration["AWS:Region"])
};
var awsCredentials = new BasicAWSCredentials(
builder.Configuration["AWS:AccessKey"],
builder.Configuration["AWS:SecretKey"]
);
var s3Client = new AmazonS3Client(awsCredentials, awsOptions);
builder.Services.AddSingleton<IAmazonS3>(s3Client);
This snippet of code sets up and configures an Amazon S3 client using the AWS SDK for .NET. Here is breakdown of each part.
– AmazonS3Config
: This is a configuration class for Amazon S3. It allows you to specify settings for the S3 client, such as the region where your S3 bucket is located.
– RegionEndpoint
: This property sets the AWS region for the S3 client. It’s retrieved from the configuration settings (builder.Configuration["AWS:Region"]
), which is defined in your appsettings.json file
–BasicAWSCredentials
: This class holds the access key and secret key needed to authenticate with AWS services.
– builder.Configuration["AWS:AccessKey"]
and builder.Configuration["AWS:SecretKey"]
: These are the access key and secret key for your AWS account, respectively, fetched from the configuration settings. These keys are used to authenticate requests to AWS services.
– AmazonS3Client
: This is the client used to interact with Amazon S3. It requires credentials and configuration options to be instantiated. In this case, it uses the awsCredentials
for authentication and awsOptions
for configuration.
– builder.Services.AddSingleton<IAmazonS3>(s3Client)
: This line registers the AmazonS3Client
instance as a singleton service in the dependency injection container. The IAmazonS3
interface is used for dependency injection, allowing you to inject the S3 client into other classes or services that require it.
- Upon including the above code, your program.cs file would appear as shown below.
using Amazon.Runtime;
using Amazon.S3;
using Amazon;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Configure AWS S3 client
var awsOptions = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(builder.Configuration["AWS:Region"])
};
var awsCredentials = new BasicAWSCredentials(
builder.Configuration["AWS:AccessKey"],
builder.Configuration["AWS:SecretKey"]
);
var s3Client = new AmazonS3Client(awsCredentials, awsOptions);
builder.Services.AddSingleton<IAmazonS3>(s3Client);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run()
- Next, head to the controllers folder and create an empty API controller then add below code to it.
[Route("api/[controller]")]
[ApiController]
public class FileHandlingController : ControllerBase
{
private readonly IAmazonS3 _s3Client;
private readonly IConfiguration _configuration;
public FileHandlingController(IAmazonS3 s3Client, IConfiguration configuration)
{
_s3Client = s3Client;
_configuration = configuration;
}
[HttpGet("DownloadFile")]
public async Task<IActionResult> DownloadFile(CancellationToken cancellationToken)
{
var fileName = "your file name";
try
{
var responseStream = await GetFileStreamAsync(fileName);
return new FileStreamResult(responseStream, "image/jpeg")
{
FileDownloadName = "download.jpeg"
};
}
catch (AmazonS3Exception ex)
{
return NotFound(new { Error = ex.Message });
}
catch (Exception ex)
{
return StatusCode(500, new { Error = ex.Message });
}
}
private async Task<Stream> GetFileStreamAsync(string fileName)
{
var request = new GetObjectRequest
{
BucketName = _configuration["AWS:BucketName"],
Key = fileName
};
var response = await _s3Client.GetObjectAsync(request);
return response.ResponseStream;
}
}
Here is the explanation of the both methods
- DownloadFile Method
– Purpose: Handles a GET request to download a file from Amazon S3.
– Process:
1. CallsGetFileStreamAsync
to get the file stream.
2. Returns the file with a MIME type ofimage/jpeg
and a default download name. (note – make sure to change mime type according to your file type)
3. Handles errors by returning appropriate HTTP status codes and error messages. - GetFileStreamAsync Method
– Purpose: Retrieves a file from Amazon S3 and returns it as a stream.
– Process:
1. Creates a request for the file using its name and bucket name.
2. Sends the request to S3 and gets the file stream.
3. Returns the file stream.
Run your project, execute the GetDownloadFile api and download your file.
🎉 Ta-da! Your file has been downloaded successfully. Enjoy!
Thank You !
Learn more How to Download File from S3 bucket with using API