How to Upload Files Directly to Amazon S3 from ASP.NET Application

How to Upload Files Directly to Amazon S3 from ASP.NET Application
As a data scientist or software engineer, you may often have to deal with large datasets. Storing and retrieving these files can be a challenge. Amazon S3 (Simple Storage Service) offers an efficient, scalable, and secure solution for this. This blog post will guide you on how to upload files directly to Amazon S3 from an ASP.NET application.
What is Amazon S3?
Amazon S3 is a scalable storage service offered by Amazon Web Services (AWS). It allows for storing and retrieving any amount of data at any time, from anywhere on the web. It is designed for durability, providing 99.999999999% (11 9’s) of durability.
Prerequisites
- An AWS account with access to S3.
- An ASP.NET application.
- AWS SDK for .NET.
Step 1: Setting up AWS Credentials
To interact with AWS services, you need to set up your AWS credentials. Create an IAM user with programmatic access, and attach the AmazonS3FullAccess policy to it. Note down the ‘Access key ID’ and ‘Secret access key’.
Step 2: Installing AWS SDK for .NET
Use the NuGet Package Manager to install the AWS SDK for .NET in your ASP.NET application:
PM> Install-Package AWSSDK.S3
Step 3: Configuring AWS SDK for .NET
In the Web.config
file of your ASP.NET application, add the AWS settings with your credentials:
<appSettings>
<!-- AWS settings -->
<add key="AWSAccessKey" value="YOUR_ACCESS_KEY"/>
<add key="AWSSecretKey" value="YOUR_SECRET_KEY"/>
<add key="AWSRegion" value="YOUR_REGION"/>
</appSettings>
Step 4: Creating the File Upload Function
In your application, create a function that uses the Amazon S3 client’s PutObjectAsync
method to upload a file:
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;
using System.IO;
using System.Threading.Tasks;
public async Task UploadFileToS3(string bucketName, string filePath)
{
var config = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.GetBySystemName(WebConfigurationManager.AppSettings["AWSRegion"]),
};
var client = new AmazonS3Client(WebConfigurationManager.AppSettings["AWSAccessKey"], WebConfigurationManager.AppSettings["AWSSecretKey"], config);
try
{
using var fileTransferUtility = new TransferUtility(client);
await fileTransferUtility.UploadAsync(filePath, bucketName);
Console.WriteLine("Upload completed");
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
Replace YOUR_ACCESS_KEY
, YOUR_SECRET_KEY
, and YOUR_REGION
with your actual AWS credentials and region.
Step 5: Using the File Upload Function
To upload a file, call the UploadFileToS3
function with the name of the S3 bucket and the path to the file you want to upload:
await UploadFileToS3("myBucket", "C:\\path\\to\\myFile.txt");
That’s it! You have successfully uploaded a file from your ASP.NET application to Amazon S3.
To conclude, leveraging Amazon S3 for file storage can greatly enhance the data handling capabilities of your ASP.NET applications. The AWS SDK for .NET simplifies the process by providing intuitive methods and classes. Happy coding!
If you found this article helpful, please, share it with your colleagues. Stay tuned for more exciting “how-to” guides and explanations on data science and software engineering topics.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.