Read and Write to Amazon S3 with ASP.NET Core
Introduction
Amazon S3, or Simple Storage Service, is a central storage service within Amazon Web Services (AWS) Cloud. It lets developers store and retrieve files as objects for cloud-based applications.
This service is supported by various programming languages through AWS tools, allowing smooth integration of storage capabilities.
In this context, we explore using S3 for file handling via the AWS SDK for ASP.NET Core.
By setting up proper access permissions through IAM policies, we can ensure secure usage of the S3 bucket. Our goal is to enable easy file upload and retrieval, enhancing user experience.
The S3 storage, organized into buckets, is managed by IAM policies that control resource access.
To Read from / Write to AWS S3 Storage, we go through the following 3 Steps -
Installing AWSSDK.S3
We start by adding the necessary AWS SDK package, enabling essential libraries for interaction with S3. The package sets the foundation for our subsequent actions.
dotnet add package AWSSDK.S3 --version 3.5.5.2
Implementing S3 Read and Write
By creating an S3Client and providing region details, we establish the connection.
The AddItem method reads the uploaded file, constructs a PutObjectRequest containing metadata, and utilizes PutObjectAsync to store the file in the bucket.
The following code shows how we can write a file to Amazon S3 -
public async Task<string> AddItem(IFormFile file, string readerName)
{
string fileName = file.FileName;
string objectKey = $"{FOLDER_NAME}/{readerName}/{fileName}";
using (Stream fileToUpload = file.OpenReadStream())
{
var putObjectRequest = new PutObjectRequest();
putObjectRequest.BucketName = BUCKET_NAME;
putObjectRequest.Key = objectKey;
putObjectRequest.InputStream = fileToUpload;
putObjectRequest.ContentType = file.ContentType;
var response = await s3Client.PutObjectAsync(putObjectRequest);
return GeneratePreSignedURL(objectKey);
}
}
To enhance security, we generate a presigned URL for time-bound access.
public string GeneratePreSignedURL(string objectKey)
{
var request = new GetPreSignedUrlRequest
{
BucketName = BUCKET_NAME,
Key = objectKey,
Verb = HttpVerb.GET,
Expires = DateTime.UtcNow.AddHours(DURATION)
};
string url = s3Client.GetPreSignedURL(request);
return url;
}
Alternatively, we can also read an individual object from the S3 Bucket and return the bytes. The following code shows how -
public async Task<byte[]> GetItem(string keyName)
{
GetObjectResponse response = await client.GetObjectAsync(BUCKET_NAME, keyName);
MemoryStream memoryStream = new MemoryStream();
using Stream responseStream = response.ResponseStream
responseStream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
Conclusion
In summary, we’ve explored and implemented a file handling solution using Amazon S3 and the AWSSDK.S3 library in ASP.NET Core. This approach simplifies file uploads and retrievals for cloud-based applications.
While our implementation works well for centralized file storage, user-centric scenarios require tailored adjustments to IAM policies.
This adaptation aligns with a role-based resource access model, providing personalized access paths within the S3 bucket.
You can read the complete article where I have explained all of these in detail here — https://referbruv.com/blog/implementing-a-simple-file-upload-to-amazon-s3-using-aspnet-core/