Deploy MiniIO on Ubuntu20.04

Create logical volume for minio

create a logical volume and mount it

1
2
3
4
5
6
7
8
sudo lvcreate hdd-vg -L 5T -n minio
sudo mkfs.ext4 /dev/hdd-vg/minio
sudo mkdir /mnt/minio

# add mounting uuid for lv
sudo vim /etc/fstab

sudo mount -a

create data folder

1
2
sudo mkdir /mnt/minio/data
sudo mkdir /mnt/minio/logs

create minio user

1
2
sudo useradd -r -s /sbin/nologin minio
sudo chown -R minio:minio /mnt/minio

Download and Config

download minio

1
2
3
4
wget https://dl.min.io/server/minio/release/linux-amd64/minio

chmod +x minio
sudo mv minio /usr/local/bin/minio

sudo vim /mnt/minio/minio.env

1
2
3
4
5
6
7
8
9
10
11
12
13
14
MINIO_VOLUMES="/mnt/minio/data"
MINIO_OPTS="--address :9000 --console-address :9090"

MINIO_ROOT_USER="xingfu"
MINIO_ROOT_PASSWORD="password"

MINIO_ACCESS_KEY="YOUR-ACCESS-KEY"
MINIO_SECRET_KEY="YOUR-SECRET-KEY"

MINIO_REGION="ap-southeast-1"

MINIO_SERVER_URL="http://0.0.0.0:9000"

# MINIO_BROWSER_REDIRECT_URL="https://minio.example.com/minio/ui"

sudo vim /etc/systemd/system/minio.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[Unit]
Description=MinIO Object Storage
Documentation=https://docs.min.io
Wants=network-online.target
After=network-online.target
AssertFileIsExecutable=/usr/local/bin/minio

[Service]
User=minio
Group=minio
WorkingDirectory=/mnt/minio
EnvironmentFile=/mnt/minio/minio.env
ExecStartPre=/usr/bin/test -n "$MINIO_VOLUMES"
ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES
Restart=always
RestartSec=5
LimitNOFILE=1048576
TasksMax=infinity
ProtectSystem=full
ProtectKernelTunables=yes
RestrictProcFS=yes
TimeoutStopSec=infinity
SendSIGKILL=no
SuccessExitStatus=0
StandardOutput=journal
StandardError=inherit

[Install]
WantedBy=multi-user.target
1
2
3
4
sudo systemctl daemon-reload
sudo systemctl enable minio
sudo systemctl start minio
sudo systemctl status minio

Use Nginx For HTTPS

Nginx服务器反向代理MinIO配置

1
2
3
4
sudo mkdir -p /var/www/minio.example.com/html
sudo chown -R $USER:$USER /var/www/minio.example.com/html
sudo chmod -R 755 /var/www/minio.example.com
sudo vim /etc/nginx/sites-available/minio.example.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
server {
client_max_body_size 0;
listen 8443 ssl;

root /var/www/minio.example.com;
index index.html index.htm index.nginx-debian.html;

server_name minio.example.com;

ssl_certificate /etc/letsencrypt/live/minio.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/minio.example.com/privkey.pem;

ignore_invalid_headers off;
proxy_buffering off;
proxy_request_buffering off;

location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_connect_timeout 300;
# Default is HTTP/1, keepalive is only enabled in HTTP/1.1
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding off;
proxy_pass http://127.0.0.1:9000; # 假设 MinIO console 运行在端口 9090
}

location /minio/ui/ {
client_max_body_size 0;
rewrite ^/minio/ui/(.*) /$1 break;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;

# This is necessary to pass the correct IP to be hashed
real_ip_header X-Real-IP;

proxy_connect_timeout 300;

# To support websockets in MinIO versions released after January 2023
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Some environments may encounter CORS errors (Kubernetes + Nginx Ingress)
# Uncomment the following line to set the Origin request to an empty string
# proxy_set_header Origin '';

chunked_transfer_encoding off;

proxy_pass http://127.0.0.1:9090;
}
}

edit the MINIO_DOMAIN in minio.config , according to the domain name above

1
sudo ln -s /etc/nginx/sites-available/minio.example.com /etc/nginx/sites-enabled/
1
sudo certbot --nginx -d minio.example.com

MinIO Client for MacOS

Install mc

1
brew install minio/stable/mc

Configure mc

1
mc alias set sg https://minio.example.com name password

Show all servers

1
mc alias list

create bucket

1
mc mb myminio/mybucket

delete bucket

1
mc rb myminio/mybucket

bucket policys

  1. Private : only owner can read, upload, and download
  2. Download: others can only read and download
  3. Upload: others can only upload
  4. Public: others can read, upload and download

set policy for bucket:

1
mc anonymous set download  myminio/test

Upload:

1
mc cp path_to_file_name myminio/bucketname

Share (7 days max)

1
2
mc share download myminio/bucketname/path_to_file_name
mc share download --recursive --expire=120h minio1/env-tools/

Or by web (if have permision)

hostname/bucket/path_to_file_name

remove file from bucket

1
mc rm myminio/mybucket/documents/report.pdf

Typora auto upload

upload script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
from datetime import datetime
from minio import Minio
from minio.error import S3Error

bucket_name = "**"
minio_url = "**"
access_key = "**"
secret_key = "**"


def upload_to_minio(file_path, bucket_name, minio_url, access_key, secret_key):
client = Minio(
minio_url, access_key=access_key, secret_key=secret_key, secure=True
) # 使用 secure=False 如果是 HTTP

# 获取当前日期,格式为 yyyy-mm-dd
today = datetime.now().strftime("%Y-%m-%d")
object_name = today + "/" + file_path.split("/")[-1] # 日期作为文件夹

try:
# 上传文件。
client.fput_object(bucket_name, object_name, file_path)
# 返回文件 URL。
return client.presigned_get_object(bucket_name, object_name)
except S3Error as e:
print("上传失败:", e)
return None


if __name__ == "__main__":
# 获取命令行参数
if len(sys.argv) < 2:
print("需要提供文件路径")
sys.exit(1)
file_path = sys.argv[1]
url = upload_to_minio(file_path, bucket_name, minio_url, access_key, secret_key)
if url:
print(url)

Load script:

1
source /Users/username/minio/venv/bin/activate && python /Users/username/minio/upload_to_minio.py