Running AWS S3 Locally with MinIO for File Storage

Run AWS S3 Locally with MinIO - Easy Setup & Step-by-Step Guide

Amazon S3 (Simple Storage Service) is a widely used object storage service provided by AWS. However, during development and testing, using AWS S3 directly can incur costs and require internet access. To overcome this, MinIO provides an S3-compatible local storage solution that developers can use to test file uploads, downloads, and integration with applications like .NET.

This guide will cover:

  • Installing MinIO for S3-compatible storage
  • Configuring and testing file uploads and downloads
  • Implementing S3 storage in .NET applications

1. Installing MinIO for S3-Compatible Storage

1.1 Prerequisites

Ensure you have the following installed:

  • Docker (for containerized setup)
  • MinIO Server and Client
  • .NET SDK (if integrating with a .NET application)

1.2 Installing MinIO via Docker

To quickly set up MinIO locally using Docker, run the following command:

mkdir -p ~/minio/data

docker run -p 9000:9000 -p 9090:9090 \
  -e "MINIO_ROOT_USER=admin" \
  -e "MINIO_ROOT_PASSWORD=admin123" \
  -v ~/minio/data:/data \
  --name minio \
  quay.io/minio/minio server /data --console-address ":9090"

1.3 Accessing MinIO Console

Once MinIO is running, open a browser and navigate to:

http://localhost:9090

Login with:

  • Username: admin
  • Password: admin123

1.4 Installing MinIO Client (mc)

The MinIO Client (mc) is a command-line tool for managing buckets and objects. Install it with:

curl -O https://dl.min.io/client/mc/release/linux-amd64/mc
chmod +x mc
sudo mv mc /usr/local/bin/

Now, configure mc to use the local MinIO server:

mc alias set local http://localhost:9000 admin admin123

Verify the connection:

mc admin info local

2. Configuring and Testing File Uploads and Downloads

2.1 Creating a Bucket

Use the MinIO client to create a bucket:

mc mb local/mybucket

2.2 Uploading Files

To upload a file:

mc cp example.txt local/mybucket/

2.3 Downloading Files

To download a file:

mc cp local/mybucket/example.txt ./

2.4 Listing Files

To list files in a bucket:

mc ls local/mybucket

3. Implementing S3 Storage in .NET Applications

3.1 Installing AWS SDK for .NET

To integrate MinIO with .NET, install the AWS SDK:

dotnet add package AWSSDK.S3

3.2 Configuring MinIO in .NET

Modify appsettings.json to configure MinIO:

{
  "AWS": {
    "ServiceURL": "http://localhost:9000",
    "AccessKey": "admin",
    "SecretKey": "admin123",
    "BucketName": "mybucket"
  }
}

3.3 Uploading Files in .NET

Create a helper class for uploading files:

using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.IO;
using System.Threading.Tasks;

public class S3Service
{
    private readonly IAmazonS3 _s3Client;
    private readonly string _bucketName;

    public S3Service(IAmazonS3 s3Client, string bucketName)
    {
        _s3Client = s3Client;
        _bucketName = bucketName;
    }

    public async Task UploadFileAsync(string filePath)
    {
        var putRequest = new PutObjectRequest
        {
            BucketName = _bucketName,
            Key = Path.GetFileName(filePath),
            FilePath = filePath,
            ContentType = "application/octet-stream"
        };

        await _s3Client.PutObjectAsync(putRequest);
        Console.WriteLine("File uploaded successfully!");
    }
}

3.4 Downloading Files in .NET

public async Task DownloadFileAsync(string fileName, string destinationPath)
{
    var getRequest = new GetObjectRequest
    {
        BucketName = _bucketName,
        Key = fileName
    };

    using var response = await _s3Client.GetObjectAsync(getRequest);
    await using var responseStream = response.ResponseStream;
    await using var fileStream = File.Create(destinationPath);
    await responseStream.CopyToAsync(fileStream);
    Console.WriteLine("File downloaded successfully!");
}

3.5 Registering S3 Client in .NET Core

Register the S3 client in Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<IAmazonS3>(_ =>
    new AmazonS3Client("admin", "admin123", new AmazonS3Config
    {
        ServiceURL = "http://localhost:9000",
        ForcePathStyle = true
    }));

builder.Services.AddSingleton<S3Service>();

var app = builder.Build();
app.Run();

Conclusion

In this guide, we covered:

✅ Installing MinIO for local S3-compatible storage ✅ Configuring and testing file uploads and downloads using the MinIO client ✅ Implementing S3 storage in .NET applications using AWS SDK

MinIO provides a powerful alternative to AWS S3 for local development and testing. By using it with .NET applications, developers can simulate cloud storage without extra costs. 🚀

Sandip Mhaske

I’m a software developer exploring the depths of .NET, AWS, Angular, React, and digital entrepreneurship. Here, I decode complex problems, share insightful solutions, and navigate the evolving landscape of tech and finance.

Post a Comment

Previous Post Next Post