# MinIO

## What is MinIO

MinIO is a high-performance, open-source object storage system designed to store unstructured data such as photos, videos, log files, backups, and container/VM images. It is fully compatible with the Amazon S3 API, making it easy to integrate with applications and tools that already support S3.

## Install MinIO on Docker

To learn more or explore advanced container setup options, check out the official MinIO documentation here:[ https://min.io/docs/minio/container/index.html](https://min.io/docs/minio/container/index.html)

### Prerequisites

Before you start, ensure you have:

* Docker installed on your system. If not, install Docker from the [official Docker website](https://www.docker.com/)
* At least one available port (default: 9000 for API, 9001 for Console)

### Step-by-Step Installation

1. **Pull the MinIO Image:**
   1. Open a terminal or command prompt and run the following command to pull the MinIO Docker image: `docker pull minio/minio`. This downloads the official MinIO image from Docker Hub.
2. **Run MinIO Container:**
   1. **Standalone Mode:**
      1. Create a Data Directory: `mkdir C:\minio-data`
      2. Run this Docker command: `docker run -d --name minio_local -p 9000:9000 -p 9001:9001 -e "MINIO_ROOT_USER=minioadmin" -e "MINIO_ROOT_PASSWORD=minioadmin" -v C:\minio-data:/data minio/minio server /data --console-address ":9001"`
   2. **Distributed Mode (Simulated on a Single Machine):**
      1. Create 4 Data Folders: `mkdir C:\minio-distributed\data1, C:\minio-distributed\data2, C:\minio-distributed\data3, C:\minio-distributed\data4`
      2. Run this Docker command: `docker run -p 9000:9000 -p 9001:9001 -v C:\minio-distributed\data1:/data1 -v C:\minio-distributed\data2:/data2 -v C:\minio-distributed\data3:/data3 -v C:\minio-distributed\data4:/data4 --name minio-distributed minio/minio server /data1 /data2 /data3 /data4 --console-address ":9001"`
3. **Verify Redis is Running:**
   * Open a terminal or command prompt and run the following command to verify the MinIO container is running: `docker ps`. You should see a container named “minio\_local” or “minio-distributed” running.
4. **Access MinIO:**
   * Web Console: Open <http://localhost:9001>&#x20;
   * Port 9000 is API port
   * Login with the credentials:&#x20;
     1. Username: minioadmin
     2. Password: minioadmin

<figure><img src="/files/q8arvyRpxMiehlsTjrlD" alt=""><figcaption><p>MinIO Login Page</p></figcaption></figure>

<figure><img src="/files/aaKnguP9A7lZ8IJhtec8" alt=""><figcaption><p>MinIO Object Dashboard</p></figcaption></figure>

## MinIO Client

The MinIO Client (mc) is a command-line tool for managing MinIO and other S3-compatible storage services. It's essential for free users because it offers full access to manage buckets, files, and advanced features like policies and replication—capabilities not available in the basic web console.

### Download MinIO Client

* To interact with your MinIO server from the command line, please download the MinIO Client (mc) for Windows: <https://dl.min.io/client/mc/release/windows-amd64/mc.exe>&#x20;
* For detailed usage and commands, refer to the MinIO Client documentation:\
  <https://min.io/docs/minio/linux/reference/minio-mc.html>

### Running the following in the command prompt or powershell:

```
\path\to\mc.exe --help
```

This displays help information for the mc.exe executable.

## Sample Use Case

### MinIO File Management System (PHP)

#### index.php - Main Interface

* File Upload: Handles multipart form uploads
* Automatic Organization: Sorts files into folders by extension
* File Listing: Displays all files in the bucket
* Download Links: Provides secure download links

#### download.php - Secure Download Handler

* Pre-signed URLs: Generates temporary, secure download links
* Access Control: Ensures files remain private
* Automatic Redirect: Seamlessly redirects users to the actual file

#### How Files Are Organized

<figure><img src="/files/LhRlEeg7ktDKytIt9ic3" alt=""><figcaption></figcaption></figure>

#### Prerequisites

**PHP Dependencies:**

```
composer require aws/aws-sdk-php vlucas/phpdotenv
```

**MinIO Server**

* MinIO server running (default:[ http://localhost:9000](http://localhost:9000))
* Admin credentials (default: minioadmin/minioadmin)
* Created bucket (example: my-private-bucket)

#### Environment Configuration

**Create a .env file in your project root:**

```
MINIO_ENDPOINT=http://localhost:9000
MINIO_KEY=minioadmin
MINIO_SECRET=minioadmin
MINIO_BUCKET=my-private-bucket
MINIO_REGION=us-east-1
```

**Configuration Parameters Explained**

| Parameter       | Description           | Example                 |
| --------------- | --------------------- | ----------------------- |
| MINIO\_ENDPOINT | MinIO server URL      | <http://localhost:9000> |
| MINIO\_KEY      | Access key (username) | minioadmin              |
| MINIO\_SECRET   | Secret key (password) | minioadmin              |
| MINIO\_BUCKET   | Target bucket name    | my-private-bucket       |
| MINIO\_REGION   | AWS region format     | us-east-1               |

#### Code Breakdown

**index.php**

**Dependencies and Setup:**

```php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Dotenv\Dotenv;
```

* Uses the AWS SDK for PHP to interact with MinIO (S3-compatible)
* Loads environment variables using DotEnv

**MinIO Client Configuration:**

```php
$s3 = new S3Client([
    'version'     => 'latest',
    'region'      => $_ENV['MINIO_REGION'],
    'endpoint'    => $_ENV['MINIO_ENDPOINT'],
    'use_path_style_endpoint' => true,
    'credentials' => [
        'key'    => $_ENV['MINIO_KEY'],
        'secret' => $_ENV['MINIO_SECRET'],
    ],
]);
```

**Key Configuration Details:**

* use\_path\_style\_endpoint => true: Required for MinIO compatibility (uses endpoint/bucket/object instead of bucket.endpoint/object)
* endpoint: Points to your MinIO server
* credentials: Authentication using access key and secret

**File Upload Logic:**

```php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $file = $_FILES['file'];
    $filename = $file['name'];
    $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
   
    // Folder selection based on extension
    switch ($extension) {
        case 'jpg':
        case 'jpeg':
        case 'png':
        case 'gif':
            $folder = 'images/';
            break;
        // ... other cases
    }
   
    $key = $folder . $filename;
   
    $s3->putObject([
        'Bucket' => $bucket,
        'Key'    => $key,
        'SourceFile' => $file['tmp_name'],
        'ACL'    => 'private'
    ]);
}
```

**Upload Process:**

1. Receives uploaded file via POST request
2. Extracts file extension
3. Determines appropriate folder based on extension
4. Uploads file to MinIO with putObject() method
5. Sets ACL to 'private' for security

**File Listing:**

```php
$objects = $s3->listObjectsV2(['Bucket' => $bucket]);
```

* Uses listObjectsV2() to retrieve all objects in the bucket
* Displays files with download links

**Simple User Interface:**

```php
<h2>Upload File</h2>
<form method="POST" enctype="multipart/form-data">
  <input type="file" name="file" required>
  <button type="submit">Upload</button>
</form>


<h2>Files in Bucket</h2>
<ul>
<?php
if (!empty($objects['Contents'])) {
    foreach ($objects['Contents'] as $obj) {
        $key = urlencode($obj['Key']);
        echo "<li>{$obj['Key']} - <a href='download.php?file={$key}'>Download</a></li>";
    }
} else {
    echo "<li>No files found.</li>";
}
?>
</ul>
```

#### download.php

**Pre-signed URL Generation:**

```php
$cmd = $s3->getCommand('GetObject', [
    'Bucket' => $bucket,
    'Key'    => $filename
]);


$request = $s3->createPresignedRequest($cmd, '+5 minutes');
$url = (string) $request->getUri();


header("Location: {$url}");
```

**Security Features:**

* Generates temporary pre-signed URLs valid for 5 minutes
* No direct file access - all downloads go through MinIO
* URLs automatically expire for security
* Redirects user to the pre-signed URL


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://technical-gitbook.kosontechnology.com/server/minio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
