Introduction
Amazon S3 (Simple Storage Service) is a widely used object storage service provided by AWS. It allows you to store and retrieve any amount of data at any time from anywhere on the web. In this article, we'll explore how to use C# with AWS S3 using the AWS SDK for .NET.
You will learn.
- How to install and configure the AWS SDK.
- Uploading files to an S3 bucket.
- Downloading files from an S3 bucket.
- Listing objects in a bucket.
- Deleting objects from a bucket.
Let's dive in!
1. Setting Up Your Project
First, create a new C# console project or use an existing project.
Install AWS SDK for .NET
Use NuGet Package Manager Console.
Install-Package -Name AWSSDK.S3
This package provides all the APIs necessary to work with Amazon S3.
2. Configuration
You need to configure AWS credentials (Access Key ID and Secret Access Key) and the region.
You can do this.
- Using AWS credential profiles stored in ~/.aws/credentials.
- Or programmatically
Example. Programmatic Configuration.
var s3Client = new AmazonS3Client(
"your-access-key-id",
"your-secret-access-key",
RegionEndpoint.USEast1
);
Important: Never hardcode credentials in production applications. Use IAM roles, environment variables, or AWS Secrets Manager for security.
3. Uploading a File to S3
Example Code
public static async Task UploadFileAsync(string filePath, string bucketName, string keyName)
{
var s3Client = new AmazonS3Client(RegionEndpoint.USEast1);
var fileTransferUtility = new TransferUtility(s3Client);
await fileTransferUtility.UploadAsync(filePath, bucketName, keyName);
Console.WriteLine("Upload completed");
}
Explanation
- TransferUtility simplifies file uploads.
- UploadAsync automatically handles multipart uploads for large files.
Call it like this.
await UploadFileAsync(
"C:\\temp\\example.txt",
"your-bucket-name",
"folder/example.txt"
);
4. Downloading a File from S3
Example Code
public static async Task DownloadFileAsync(string bucketName, string keyName, string destinationPath)
{
var s3Client = new AmazonS3Client(RegionEndpoint.USEast1);
var fileTransferUtility = new TransferUtility(s3Client);
await fileTransferUtility.DownloadAsync(destinationPath, bucketName, keyName);
Console.WriteLine("Download completed");
}
Explanation
Downloads an object and saves it to a specified local path.
Call it like this.
await DownloadFileAsync(
"your-bucket-name",
"folder/example.txt",
"C:\\temp\\downloaded.txt"
);
5. Listing Objects in a Bucket
Example Code
public static async Task ListObjectsAsync(string bucketName)
{
var s3Client = new AmazonS3Client(RegionEndpoint.USEast1);
var response = await s3Client.ListObjectsV2Async(new ListObjectsV2Request
{
BucketName = bucketName
});
foreach (var obj in response.S3Objects)
{
Console.WriteLine($"{obj.Key} (Size: {obj.Size} bytes)");
}
}
Explanation
- Lists all objects in a specified bucket.
- ListObjectsV2Request improves performance and reliability over the original ListObjects API.
Call it like this.
await ListObjectsAsync("your-bucket-name");
6. Deleting an Object from S3
Example Code
public static async Task DeleteObjectAsync(string bucketName, string keyName)
{
var s3Client = new AmazonS3Client(RegionEndpoint.USEast1);
await s3Client.DeleteObjectAsync(new DeleteObjectRequest
{
BucketName = bucketName,
Key = keyName
});
Console.WriteLine("Object deleted successfully");
}
Explanation
- Deletes a single object from the specified bucket.
- Always confirm permissions allow s3:DeleteObject.
Call it like this.
await DeleteObjectAsync(
"your-bucket-name",
"folder/example.txt"
);
Best Practices
- Secure credentials: Use environment variables, IAM roles, or Secrets Manager.
- Exception handling: Always wrap AWS SDK calls in try-catch blocks.
- Regions: Match your bucket's AWS region to avoid extra latency.
- Logging: Use logging frameworks like Serilog or NLog to monitor S3 operations.
- Retries: AWS SDK automatically retries on transient failures, but you can customize retry policies.
Conclusion
Using AWS S3 with C# is straightforward thanks to the AWS SDK for .NET. In this article, you learned how to upload, download, list, and delete files in S3 buckets with clear and practical examples.
This opens the door to building scalable applications that leverage S3 for backups, file storage, CDN integration, and more.