Working with S3 and Cloud Storage in C# (.NET): AWS SDK Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with S3 and Cloud Storage in C# (.NET)



Modern cloud technologies require reliable data storage and management. One of the de facto standards for object storage is the Amazon S3 (Simple Storage Service) protocol. In the C# (.NET) ecosystem, the official AWS SDK for .NET library (package AWSSDK.S3) is used to work with S3 and any S3-compatible storage (Yandex Object Storage, MinIO, DigitalOcean Spaces, VK Cloud).



In this article, we will cover in detail how to add the library, configure the client, perform basic operations (upload, download, delete, listing), and adapt the code for working with local S3 emulators such as MinIO.



Installing the Library



First, add the NuGet package to your project:



dotnet add package AWSSDK.S3


Or via the Visual Studio package manager:



Install-Package AWSSDK.S3


Creating and Configuring the S3 Client



The library provides the AmazonS3Client class, which can be configured to work with any S3-compatible storage. Let's consider two options: connecting to Amazon S3 and to a local MinIO instance.



Connecting to Amazon S3



using Amazon;using Amazon.S3;using Amazon.Runtime;

var credentials = new BasicAWSCredentials("your_ACCESS_KEY", "your_SECRET_KEY");var config = new AmazonS3Config{ RegionEndpoint = RegionEndpoint.EUWest1 // or another region};

using var client = new AmazonS3Client(credentials, config);


Connecting to MinIO (Local S3 Server)



using Amazon.S3;using Amazon.Runtime;

var credentials = new BasicAWSCredentials("minioadmin", "minioadmin");var config = new AmazonS3Config{ ServiceURL = "http://localhost:9000", // MinIO address ForcePathStyle = true, // required for MinIO UseHttp = true};

using var client = new AmazonS3Client(credentials, config);


Basic S3 Operations



All operations are performed asynchronously via client methods. Let's look at the key scenarios.



1. Uploading a File (Upload)



using Amazon.S3;using Amazon.S3.Model;

public async Task UploadFileAsync(IAmazonS3 client, string bucketName, string filePath, string key){ var request = new PutObjectRequest { BucketName = bucketName, Key = key, FilePath = filePath };

var response = await client.PutObjectAsync(request); Console.WriteLine($"File uploaded. ETag: {response.ETag}");}


2. Downloading a File (Download)



public async Task DownloadFileAsync(IAmazonS3 client, string bucketName, string key, string downloadPath){    var request = new GetObjectRequest    {        BucketName = bucketName,        Key = key    };

using var response = await client.GetObjectAsync(request); await response.WriteResponseStreamToFileAsync(downloadPath, false, CancellationToken.None); Console.WriteLine($"File saved to {downloadPath}");}


3. Deleting a File (Delete)



public async Task DeleteFileAsync(IAmazonS3 client, string bucketName, string key){    var request = new DeleteObjectRequest    {        BucketName = bucketName,        Key = key    };

var response = await client.DeleteObjectAsync(request); Console.WriteLine($"File deleted. HTTP status: {response.HttpStatusCode}");}


4. Listing Files (List Objects)



public async Task ListFilesAsync(IAmazonS3 client, string bucketName){    var request = new ListObjectsV2Request    {        BucketName = bucketName,        MaxKeys = 100    };

ListObjectsV2Response response; do { response = await client.ListObjectsV2Async(request); foreach (var obj in respo

Recommendations