MinIO 云存储客户端基本操作

参考文档

MINIO SERVER 的下载、安装和启动

 1wget https://dl.min.io/server/minio/release/linux-amd64/minio
 2chmod +x minio
 3
 4# linux 通过指定的用户名和密码启动服务(9000为客户端连接端口,9001为Web管理端口)
 5MINIO_ROOT_USER=admin MINIO_ROOT_PASSWORD=12345678 ./minio server /app/minio-data --address ":9000" --console-address ":9001"
 6# --address 可选,表示服务端口,默认:9000
 7# --console-address 可选,表示web管理端口,可通过 http://localhost:9001 访问
 8
 9# linux 通过默认设置启动(不推荐;用户名和密码皆为minioadmin,客户端连接端口为9000,Web管理端口随机)
10./minio server /app/repository-minio
11
12# Windows启动
13minio.exe server D:\tmp\minio-files

MINIO CLIENT 的下载、使用

1wget https://dl.min.io/client/mc/release/linux-amd64/mc
2chmod +x mc

强烈建议添加minio和mc到环境变量中!!!

MinIO云存储客户端常用操作

 1# 添加一个 minio 服务到本地:
 2# mc config host add <ALIAS> <YOUR-S3-ENDPOINT> <YOUR-ACCESS-KEY> <YOUR-SECRET-KEY> [--api API-SIGNATURE]
 3# <ALIAS>                : 服务别名,是云存储服务的简称,alias名称可以随意起
 4# <YOUR-S3-ENDPOINT>     : minio 服务的地址
 5# <YOUR-ACCESS-KEY>      : 登录用户名
 6# <YOUR-SECRET-KEY>      : 登录密码
 7# --api <API-SIGNATURE>  : 可选参数;默认是为S3v4
 8# 例如:
 9mc config host add myminio http://127.0.0.1:9000 admin 12345678
10
11# 测试连接
12mc admin info myminio
13
14
15# 查看所有的 minio 服务:
16mc config host ls
17
18
19# 查看别名为 【minio】 的服务下所有的 bucket:
20mc ls myminio
21
22
23# 在别名为 【minio】 的服务下添加一个名为 【mybucket】 的 bucket:
24mc mb myminio/mybucket
25
26
27# 为名为 【mybucket】 的 bucket 设置只读权限
28# 权限分为四种:none(默认), download(读), upload(写), public(读写)
29mc policy set download myminio/mybucket
30
31
32# 查看名为 【mybucket】 的 bucket 的权限
33mc policy get myminio/mybucket
34
35
36# 查看别名为 【minio】 的服务下、bucket 名为 【mybucket】 中的文件:
37mc ls myminio/mybucket
38
39
40# 拷贝(相当于上传)一个文件到 bucket :
41mc cp D:/tmp/北京机要跟踪主要的实际和问题.txt myminio/mybucket
42
43
44# 共享文件的下载路径:
45mc share download myminio/mybucket/img102.jpg
46
47# 查看共享的文件:
48mc share list download
49
50# 查找 bucket 中的png文件:
51mc find myminio/mybucket --name "*.jpg"
 1# 启动 minio 服务
 2minio server D:/tmp/minio-files
 3
 4# 添加一个 minio 服务到本地
 5mc config host add myminio http://127.0.0.1:9000 minioadmin minioadmin
 6
 7# 查看本地所有的 minio 服务
 8mc config host ls
 9
10# 创建名为 public 的 bucket
11mc mb myminio/public
12
13# 显示 bucket 下所有的通知
14mc event list myminio/public
15
16# 设置 bucket 匿名用户可以读取(设置 bucket 权限有四个值:none, download, upload, public)
17mc policy set download myminio/public
18
19# 设置 bucket 访问策略
20# 先创建一个策略文件,内容如下:
21vim my-policy.json
22{
23    "Version": "2012-10-17",
24    "Statement": [
25        {
26            "Effect": "Allow",
27            "Action": [
28                "s3:PutObject",
29                "s3:DeleteObject",
30                "s3:GetBucketLocation",
31                "s3:GetObject",
32                "s3:ListAllMyBuckets",
33                "s3:ListBucket"
34            ],
35            "Resource": [
36                "arn:aws:s3:::public/*"
37            ]
38        }
39    ]
40}
41# 添加访问策略:
42mc admin policy add myminio public-bucket-read-write-policy my-policy.json
43# 然后创建用户:
44mc admin user add myminio 18008423246 12345678
45# 最后为用户应用访问策略:
46mc admin policy set myminio public-bucket-read-write-policy user=18008423246
47
48################################ webhook #################################
49
50# 显示 webhook 通知
51mc admin config get myminio notify_webhook
52
53# 设置 webhook 通知 (需要重启服务才能生效)
54mc admin config set myminio notify_webhook:1 queue_limit="0" endpoint="http://127.0.0.1:9401/minio/events" queue_dir=""
55
56# 为 bucket 设置 webhook 通知
57mc event add myminio/public arn:minio:sqs::1:webhook --event put,delete
58
59################################# mqtt ###################################
60
61# 展示 mqtt 通知
62mc admin config get myminio notify_mqtt
63
64# 设置 mqtt 通知 (需要重启服务才能生效)
65# notify_mqtt后面的 primary 可以理解为通知的名称,如果只有一个通知也可不加,这是为了区分多个通知
66mc admin config set myminio notify_mqtt:primary broker="tcp://localhost:1883" topic="minio/public/events" username="admin" password="admin" queue_dir="" queue_limit="0" reconnect_interval="0s"  keep_alive_interval="0s" qos="1"
67
68# 为 bucket 添加 mqtt 通知(注意上面的设置通知时是否带上了名称)
69# --event 参考可不加,默认为:put,delete,get
70mc event add --event "put,delete" myminio/public arn:minio:sqs::primary:mqtt
71
72# 删除 mqtt 通知(注意:如果添加通知时,加上了--event 参数,删除时也要加上)
73mc event remove --event "put,delete" myminio/public arn:minio:sqs::primary:mqtt
74
75# 重置(或删除)mqtt 通知
76mc admin config reset myminio notify_mqtt:primary

MinIO多用户权限管理

先决条件

安装 mc

安装 MinIO

创建固定访问策略

mc admin policy add <minio别名> <策略别名> <策略的json文件名>

示例:

mc admin policy add myminio test-policy test-policy.json

test-policy.json 文件示例:

 1{
 2    "Version": "2012-10-17",
 3    "Statement": [
 4        {
 5            "Effect": "Allow",
 6            "Action": [ //权限列表
 7                "s3:ListAllMyBuckets", //查看所有的桶权限
 8                "s3:ListBucket", //查看桶内对象权限
 9                "s3:GetBucketLocation", //定位bucket权限
10                "s3:GetObject", //下载权限
11                "s3:PutObject", //上传权限
12                "s3:DeleteObject" //删除权限
13            ],
14            "Resource": [
15                "arn:aws:s3:::onebucket/*" //应用到的资源,arn:aws:s3表示命名空间,不要改动;onebucket/*表示只针对这个桶(可以直接用*表示所有桶)
16            ]
17        }
18    ]
19}

创建用户,设置策略

在MinIO使用上创建一个新用户:

mc admin user add <minio别名> <用户名> <密码>

为用户应用策略:

mc admin policy set <minio别名> <策略名称> user=<用户名>

禁用用户

1# 禁用用户 newuser
2mc admin user disable myminio newuser
3
4# 禁用组 newgroup
5mc admin group disable myminio newgroup

列出所有用户或组

mc admin user list <minio别名>

mc admin gourp list <minio别名>

删除策略

1[root@localhost minio]# mc admin policy remove myminio test-policy
2Removed policy `test-policy` successfully.

linux 设置为系统服务并开机启动

1vim /usr/lib/systemd/system/minio.service

内容如下:

 1[Unit]
 2Description=Minio Service
 3After=network.target
 4
 5[Service]
 6Type=simple
 7Environment=MINIO_ROOT_USER=root MINIO_ROOT_PASSWORD=12345678
 8ExecStart=/app/minio/minio server /app/repository-minio --console-address ":9001"
 9ExecReload=/bin/kill -s HUP $MAINPID
10ExecStop=/bin/kill -s QUIT $MAINPID
11PrivateTmp=true
12
13[Install]
14WantedBy=multi-user.target

重载系统服务

1systemctl daemon-reload

启动

1systemctl start minio

设置为开机自启动

1systemctl enable minio

nginx 作为 minio 前置代理服务器的设置

 1worker_processes  1;
 2
 3events {
 4    worker_connections  1024;
 5}
 6
 7http {
 8    include       mime.types;
 9    default_type  application/octet-stream;
10
11    sendfile        on;
12
13    keepalive_timeout  65;
14
15    server {
16        listen       80;
17        server_name  localhost;
18
19        location / {
20            root   html;
21            index  index.html index.htm;
22        }
23
24        location ^~ /api {
25            proxy_set_header    Host $host;
26            proxy_set_header    X-Real-IP $remote_addr;
27            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
28            proxy_set_header    X-Forwarded-Proto $scheme;
29            proxy_pass          http://127.0.0.1:8000;
30        }
31
32        location ^~ /files {
33            proxy_buffering     off;
34            proxy_set_header    Host $http_host;
35            rewrite             ^/files/(.*)$ /$1 break;
36            proxy_pass          http://localhost:9000;
37        }
38
39        error_page   500 502 503 504  /50x.html;
40        location = /50x.html {
41            root   html;
42        }
43    }
44}

例如,在mybucket(权限至少设置为download)中上传了一张图片kobe.jpg

根据上面nginx的配置,访问路径为:http://localhost/files/mybucket/kobe.jpg