Logo

dev-resources.site

for different kinds of informations.

Using MinIO Server for Local Development: A Smarter Alternative to S3

Published at
12/31/2024
Categories
node
minioserver
minio
s3
Author
thenelson_o
Categories
4 categories in total
node
open
minioserver
open
minio
open
s3
open
Author
11 person written this
thenelson_o
open
Using MinIO Server for Local Development: A Smarter Alternative to S3

File uploads are a staple of many applications, enabling users to share images, videos, documents, and more. While S3 is a popular choice for handling file storage, itโ€™s not always ideal for local development and testing. The need to make HTTP calls via the AWS SDK introduces latency, and relying on S3 during development can be slow and cumbersome. Mocking or spying during end-to-end testing might work, but it often feels like a compromise.

Thatโ€™s where MinIO Server comes inโ€”a high-performance, S3-compatible object storage solution thatโ€™s perfect for local development. MinIO offers the same S3 APIs, making it a seamless alternative to integrate into your existing workflows. In this article, weโ€™ll demonstrate how to set up and use MinIO with a Node.js application, while also highlighting its unique benefits.

Why MinIO?

  • Lightning-fast local operations instead of internet-dependent API calls
  • Identical S3 API compatibility โ€“ no code changes needed
  • A sleek web interface for manual file management
  • Cost savings on S3 requests during development
  • Perfect isolation for testing environments
  • Zero internet dependency for file operations

Setting Up MinIO with Docker

Getting started is straightforward. While you can download MinIO directly, Docker makes it even easier. Hereโ€™s how to configure MinIO using Docker Compose.

  1. Install Docker: If you donโ€™t already have Docker installed, download it here.
  2. Create a docker-compose.yml File: Add the following configuration:
version: "3"
services:
  file-storage:
    restart: always
    image: minio/minio:RELEASE.2022-06-11T19-55-32Z
    container_name: s3-file-storage
    environment:
      MINIO_ROOT_USER: admin
      MINIO_ROOT_PASSWORD: Password1234
    command: server --console-address ":9001" /file-storage-volume
    volumes:
      - file-storage-volume:/file-storage-volume
    ports:
      - 9000:9000 # Files served via this port
      - 9001:9001 # Admin console accessible via this port

volumes:
  file-storage-volume:
Enter fullscreen mode Exit fullscreen mode

Run Docker Compose:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Once running, you can access:

  • API endpoint at http://localhost:9000
  • Web console at http://localhost:9001 (login with admin/Password1234)

Integrating MinIO with Your Node.js Application

Here's where the magic happens. MinIO seamlessly replaces S3 in your development environment while keeping production untouched. Here's how to set it up:

Install Dependencies

Make sure you have the AWS SDK installed:

npm install @aws-sdk/client-s3
Enter fullscreen mode Exit fullscreen mode

Configure MinIO and S3

Use the following configuration to create a storage instance:

import { once } from "lodash";
import { S3 } from "@aws-sdk/client-s3";

const config = {
  awsRegion: "eu-west-2",
  minioBaseUrl: "http://localhost:9000",
  minioUsername: "admin",
  minioPassword: "Password1234",
};

const isDevelopmentOrTest = process.env.NODE_ENV !== "production";

const createOrGetStorageInstance = once(() => {
  if (isDevelopmentOrTest) {
    return new S3({
      endpoint: config.minioBaseUrl,
      forcePathStyle: true,
      region: config.awsRegion,
      credentials: {
        accessKeyId: config.minioUsername,
        secretAccessKey: config.minioPassword,
      },
    });
  }

  return new S3({ region: config.awsRegion });
});

export const s3 = createOrGetStorageInstance();
Enter fullscreen mode Exit fullscreen mode

This setup uses MinIO during development and seamlessly falls back to S3 in production.

Putting It All Together: File Upload Example

Here's a practical example using Express and express-fileupload:

Install Dependencies

npm install express express-fileupload
Enter fullscreen mode Exit fullscreen mode

Implement File Upload

Hereโ€™s a simple Express route for uploading files:

import express from "express";
import fileUpload from "express-fileupload";

const app = express();
app.use(fileUpload());

app.post("/upload", async (req, res) => {
  if (!req.files||!req.files.files) {
      return res.status(400).send("No files were uploaded.");
  }

  const uploadedFiles = Array.isArray(req.files.files)
    ? req.files.files
    : [req.files.files];

  const uploadedUrls = await Promise.all(
    uploadedFiles.map(async (file) => {
      const key = `uploads/${file.name}`;
      const uploadParams = {
        Bucket: "my-bucket",
        Key: key,
        Body: file.data,
        ContentType: file.mimetype,
      };

      const uploadResult = await s3.upload(uploadParams).promise();
      return uploadResult.Location;
    })
  );

  res.json({ message: "Files uploaded successfully", urls: uploadedUrls });
});

app.listen(3000, () => console.log("Server running on port 3000"));
Enter fullscreen mode Exit fullscreen mode

### Postman Example:
Below is an example of how to test this route using Postman:
Screenshot

Pro Tips for MinIO Usage

  1. Web Console Features: Take advantage of MinIO's web interface to:
    • Browse uploaded files
    • Create and manage buckets
    • Set up access policies
    • Monitor usage statistics
  2. Development Workflow:
    • Create separate buckets for different file types (images, documents, etc.)
    • Use the web console to verify uploads during development
    • Use environment variables to switch between MinIO and S3

Conclusion

MinIO Server is a game-changer for local development. It provides the perfect balance of functionality and simplicity, making your development process faster and more reliable. With identical S3 API compatibility and a user-friendly web interface, it's the ideal solution for local development and testing environments.

Remember: your production environment still uses real S3, but your development environment gets all the benefits of local, lightning-fast file operations. It's the best of both worlds!

Try it out in your next project, and you'll wonder how you ever developed without it.

Source Code ๐Ÿ“‚

GitHub Repository: https://github.com/Nedum84/minio-s3-server

Let's Connect ๐ŸŒ


What Do You Think? ๐Ÿ’ญ

Share your thoughts! Did this post help you?
Have ideas or feedback to share? Letโ€™s continue the conversation in the comments below! ๐Ÿ˜Š

s3 Article's
30 articles in total
Favicon
Building a Weather Data Collection System with AWS S3 and OpenWeather API
Favicon
Comprehensive Guide to Installing AWS CLI, Configuring It, and Downloading S3 Buckets Locally
Favicon
Stream de Arquivo PDF ou Imagem S3 - AWS
Favicon
Efficiently Deleting Millions of Objects in Amazon S3 Using Lifecycle Policy
Favicon
Uploading Files to Amazon S3 in ASP.NET Core with Razor Pages
Favicon
AWS S3 Presigned URLs: Secure and Temporary File Access Made Simple
Favicon
How to implement File uploads in Nodejs: A step by step guide
Favicon
Lee esto antes de implementar S3 y CloudFront usando Terraform.
Favicon
Lee esto antes de implementar S3 y CloudFront usando Terraform.
Favicon
๐Ÿš€ 1. Efficient Video Uploads to AWS S3 with React
Favicon
Full Stack Application Hosting in AWS
Favicon
Building an S3 Static Website with CloudFront Using Terraform
Favicon
Configure IRSA using EKS to access S3 from a POD in terraform
Favicon
Setting up IAM Anywhere using terraform
Favicon
AWS S3 System Design Concepts
Favicon
Creating an S3 Bucket in AWS and generate a pre - signed URL
Favicon
Switching to the Terraform S3 Backend with Native State File Locks
Favicon
Around the World in 15 Buckets
Favicon
My (non-AI) AWS re:Invent 24 picks
Favicon
How to Simulate AWS S3 on Your Local Machine with LocalStack
Favicon
Building Websites with Cursor and AWS.
Favicon
Configuring AWS WAF, CloudFront, and S3 Bucket for Secure Access
Favicon
Buckets? No, S3 buckets
Favicon
Download Video from s3 with Cloudfront, nodejs and react
Favicon
AWS Quick Guide - Amazon S3
Favicon
Fastest and Cheapest Ways to Delete Millions of Files from Amazon S3
Favicon
Using MinIO Server for Local Development: A Smarter Alternative to S3
Favicon
AWS CloudFront vs S3 Cross-Region Replication
Favicon
Comparison of S3 upload feature between Documenso and aws-s3-image-upload example
Favicon
Securing Your AWS EC2 and S3 Communication: Best Practices for Enhanced Security

Featured ones: