跳到正文

使用 MinIO CLI(mc)

MinIO Client(mc)是命令行工具,用法接近 Unix 的 ls、cp、find,可以连接任何兼容 S3 的对象存储。

MinIO 已停止维护 mc,官方网站不再提供下载,也不再发布安全更新。已经在用的可以继续,本文的命令在最后一个版本上实测可用。新项目建议用 AWS CLI、s5cmd 或 Rclone。

前置条件

安装

先安装 Go,再执行:

PowerShell
go install github.com/minio/mc@latest
brew install minio-mc

先安装 Go,再执行:

go install github.com/minio/mc@latest

Windows 的命令请在 PowerShell 里运行,示例里的 D:\ 换成自己的目录。

Homebrew 已把它标记为弃用,计划在 2027 年 7 月停用。社区维护的分支见 pgsty/mc,程序名为 mcli,命令相同。

Linux 上如果装过 Midnight Commander,它的程序也叫 mc,请给其中一个改名。

配置

PowerShell
mc.exe alias set s4 https://s3.bitiful.net "你的 AccessKey" "你的 SecretKey" --api S3v4
mc alias set s4 https://s3.bitiful.net "你的 AccessKey" "你的 SecretKey" --api S3v4
mc alias set s4 https://s3.bitiful.net "你的 AccessKey" "你的 SecretKey" --api S3v4

之后用 s4/桶名/路径 表示 S4 上的位置。验证:

PowerShell
mc.exe ls s4
mc.exe ls s4/my-bucket/
mc ls s4
mc ls s4/my-bucket/
mc ls s4
mc ls s4/my-bucket/

密钥保存在 ~/.mc/config.json(Windows 为 C:\Users\用户名\mc\config.json),没有加密,注意文件权限。

上传与下载

PowerShell
# 上传文件、目录
mc.exe cp D:\logo.png s4/my-bucket/images/logo.png
mc.exe cp -r D:\dist\ s4/my-bucket/site/

# 指定类型、缓存和自定义元数据,用分号分隔
mc.exe cp --attr "Cache-Control=max-age=86400;Content-Type=text/css;owner=web" D:\app.css s4/my-bucket/static/app.css

# 备份数据库并上传
mysqldump.exe mydb --result-file=D:\backup\mydb.sql
mc.exe cp D:\backup\mydb.sql s4/my-bucket/backup/mydb.sql

# 下载
mc.exe cp s4/my-bucket/images/logo.png D:\
mc.exe get s4/my-bucket/images/logo.png D:\logo.png
# 上传文件、目录
mc cp ./logo.png s4/my-bucket/images/logo.png
mc cp -r ./dist/ s4/my-bucket/site/

# 指定类型、缓存和自定义元数据,用分号分隔
mc cp --attr "Cache-Control=max-age=86400;Content-Type=text/css;owner=web" ./app.css s4/my-bucket/static/app.css

# 从标准输入上传
mysqldump mydb | gzip | mc pipe s4/my-bucket/backup/mydb.sql.gz

# 下载
mc cp s4/my-bucket/images/logo.png ./
mc get s4/my-bucket/images/logo.png ./logo.png
# 上传文件、目录
mc cp ./logo.png s4/my-bucket/images/logo.png
mc cp -r ./dist/ s4/my-bucket/site/

# 指定类型、缓存和自定义元数据,用分号分隔
mc cp --attr "Cache-Control=max-age=86400;Content-Type=text/css;owner=web" ./app.css s4/my-bucket/static/app.css

# 从标准输入上传
mysqldump mydb | gzip | mc pipe s4/my-bucket/backup/mydb.sql.gz

# 下载
mc cp s4/my-bucket/images/logo.png ./
mc get s4/my-bucket/images/logo.png ./logo.png

大文件自动分片上传。

同步目录

PowerShell
# 先预演
mc.exe mirror --dry-run --overwrite --remove D:\dist s4/my-bucket/site

# 让目标与本地一致,--remove 会删除目标里多余的文件
mc.exe mirror --overwrite --remove D:\dist s4/my-bucket/site

# 下载整个目录
mc.exe mirror s4/my-bucket/site D:\site

# 持续监听本地目录,有变化就上传
mc.exe mirror --watch D:\uploads s4/my-bucket/uploads
# 先预演
mc mirror --dry-run --overwrite --remove ./dist s4/my-bucket/site

# 让目标与本地一致,--remove 会删除目标里多余的文件
mc mirror --overwrite --remove ./dist s4/my-bucket/site

# 下载整个目录
mc mirror s4/my-bucket/site ./site

# 持续监听本地目录,有变化就上传
mc mirror --watch ./uploads s4/my-bucket/uploads
# 先预演
mc mirror --dry-run --overwrite --remove ./dist s4/my-bucket/site

# 让目标与本地一致,--remove 会删除目标里多余的文件
mc mirror --overwrite --remove ./dist s4/my-bucket/site

# 下载整个目录
mc mirror s4/my-bucket/site ./site

# 持续监听本地目录,有变化就上传
mc mirror --watch ./uploads s4/my-bucket/uploads

--remove 会删除目标中源没有的文件,S4 没有回收站,请先用 --dry-run 预演。

查看与查找

PowerShell
mc.exe ls -r s4/my-bucket/images/                                # 递归列出
mc.exe du s4/my-bucket/images                                    # 统计容量
mc.exe stat s4/my-bucket/images/logo.png                         # 元数据
mc.exe cat s4/my-bucket/notes/todo.txt                           # 查看内容
mc.exe head -n 20 s4/my-bucket/logs/app.log                      # 前 20 行
mc.exe diff D:\dist s4/my-bucket/site                            # 比较本地与桶

mc.exe find s4/my-bucket --name "*.mp4"                          # 按名称
mc.exe find s4/my-bucket --older-than 30d --larger 100MB         # 按时间和大小
mc ls -r s4/my-bucket/images/                                # 递归列出
mc du s4/my-bucket/images                                    # 统计容量
mc stat s4/my-bucket/images/logo.png                         # 元数据
mc cat s4/my-bucket/notes/todo.txt                           # 查看内容
mc head -n 20 s4/my-bucket/logs/app.log                      # 前 20 行
mc diff ./dist s4/my-bucket/site                             # 比较本地与桶

mc find s4/my-bucket --name "*.mp4"                          # 按名称
mc find s4/my-bucket --older-than 30d --larger 100MB         # 按时间和大小
mc ls -r s4/my-bucket/images/                                # 递归列出
mc du s4/my-bucket/images                                    # 统计容量
mc stat s4/my-bucket/images/logo.png                         # 元数据
mc cat s4/my-bucket/notes/todo.txt                           # 查看内容
mc head -n 20 s4/my-bucket/logs/app.log                      # 前 20 行
mc diff ./dist s4/my-bucket/site                             # 比较本地与桶

mc find s4/my-bucket --name "*.mp4"                          # 按名称
mc find s4/my-bucket --older-than 30d --larger 100MB         # 按时间和大小

移动与删除

PowerShell
mc.exe mv s4/my-bucket/a.txt s4/my-bucket/b.txt

mc.exe rm s4/my-bucket/a.txt
mc.exe rm -r --force s4/my-bucket/tmp/                           # 删除整个目录,无法恢复
mc.exe rm -r --force --older-than 30d s4/my-bucket/logs/         # 只删 30 天前的
mc mv s4/my-bucket/a.txt s4/my-bucket/b.txt

mc rm s4/my-bucket/a.txt
mc rm -r --force s4/my-bucket/tmp/                           # 删除整个目录,无法恢复
mc rm -r --force --older-than 30d s4/my-bucket/logs/         # 只删 30 天前的
mc mv s4/my-bucket/a.txt s4/my-bucket/b.txt

mc rm s4/my-bucket/a.txt
mc rm -r --force s4/my-bucket/tmp/                           # 删除整个目录,无法恢复
mc rm -r --force --older-than 30d s4/my-bucket/logs/         # 只删 30 天前的

mc mv 会先复制再删除。大文件请用 S4 的 原生移动。

分享链接

PowerShell
mc.exe share download --expire 1h s4/my-bucket/images/logo.png
mc share download --expire 1h s4/my-bucket/images/logo.png
mc share download --expire 1h s4/my-bucket/images/logo.png

有效期最长 7 天(168h)。

标签

PowerShell
mc.exe tag set s4/my-bucket/images/logo.png "env=prod&team=web"
mc.exe tag list s4/my-bucket/images/logo.png
mc tag set s4/my-bucket/images/logo.png "env=prod&team=web"
mc tag list s4/my-bucket/images/logo.png
mc tag set s4/my-bucket/images/logo.png "env=prod&team=web"
mc tag list s4/my-bucket/images/logo.png

未完成的分片上传

PowerShell
mc.exe ls --incomplete s4/my-bucket/
mc.exe rm -r --force --incomplete s4/my-bucket/
mc ls --incomplete s4/my-bucket/
mc rm -r --force --incomplete s4/my-bucket/
mc ls --incomplete s4/my-bucket/
mc rm -r --force --incomplete s4/my-bucket/

在两个存储之间复制

给其他存储也设置一个别名,就可以直接复制:

PowerShell
mc.exe alias set old https://oss-cn-hangzhou.aliyuncs.com "源的 AccessKey" "源的 SecretKey"
mc.exe mirror old/old-bucket s4/my-bucket
mc alias set old https://oss-cn-hangzhou.aliyuncs.com "源的 AccessKey" "源的 SecretKey"
mc mirror old/old-bucket s4/my-bucket
mc alias set old https://oss-cn-hangzhou.aliyuncs.com "源的 AccessKey" "源的 SecretKey"
mc mirror old/old-bucket s4/my-bucket

数据会经过运行 mc 的机器。

不适用的命令

mc admin、mc anonymous set、mc ilm、mc replicate、mc version enable、mc encrypt 等用于管理 MinIO 服务端,或依赖 S4 没有提供的功能。对应的设置请在控制台操作:

需求在控制台的位置
公开读取公共读与私有
子账户与授权子账户权限
跨域跨域设置 CORS

子账户不能创建桶,mc mb 会返回权限错误。

常见问题

The request signature we calculated does not match

检查密钥是否多了空格、本机时间是否准确。密钥含特殊字符时要加引号。

mc ls s4 是空的

只会列出子账户有授权的桶,请先 授权。

Access Denied

只授权了目录时,路径要写到授权目录,例如 s4/my-bucket/images/。

mc 打开了一个蓝色的文件管理器

运行的是 Midnight Commander。改用完整路径,例如 ~/go/bin/mc。