MinIO Docker
MinIO
MinIO 是一个高性能、兼容 S3 的对象存储,主要用于各种云原生应用、数据备份、归档等通用场景。提供身份验证,访问控制,加密等方式保护存储数据的安全。
MinIO Docker
$ docker run -d \
--name minio \
-p 9900:9000 \
-p 9901:9001 \
-v ~/data/docker/minio/data:/data \
-v ~/data/docker/minio/config:/root/.minio \
-e MINIO_ROOT_USER=tester \
-e MINIO_ROOT_PASSWORD=12345678 \
minio/minio server /data --console-address ":9001"
访问
http://192.168.56.101:9901/
概念
-
bucket: MinIO使用bucket来组织对象。bucket类似于文件系统中的文件夹或目录,其中每个bucket可以容纳任意数量的对象。
-
access key: 继承了显式附加到父用户的策略,以及附加到其父用户所属的每个组的策略。
创建 bucket
上传对象 (任何文件/文件夹)
- 上传一张图片
创建访问密钥
验证
以下使用Python验证
- 检查存储桶是否存在
- 上传文件到存储桶
- 列出存储桶中的所有对象
- 下载文件从存储桶
- 删除存储桶中的对象
from minio import Minio
from minio.error import S3Error
access_key = "jsbZVomx7aYfs8ZkWcpj"
secrets_key = "alVeNp8ZbpdWIzZeEGfLvn6UliX0UBWDRh5OiQwy"
# 初始化 MinIO 客户端
minio_client = Minio(
# MinIO 服务器地址 9900 而不是 9901,不要加 http/https前缀
endpoint="192.168.56.101:9900",
# 访问密钥
access_key=f"{access_key}",
# 安全密钥
secret_key=f"{secrets_key}",
# 如果使用 HTTPS,设置为 True
secure=False
)
bucket_name = "bucket1"
object_name = "example"
file_path = "../../data/file.txt"
download_path = "../../data/file_download.txt"
try:
# 检查存储桶是否存在
found = minio_client.bucket_exists(bucket_name)
if not found:
minio_client.make_bucket(bucket_name)
print(f"Bucket '{bucket_name}' created successfully.")
else:
print(f"Bucket '{bucket_name}' already exists.")
# 上传文件到存储桶
minio_client.fput_object(bucket_name, object_name, file_path)
print(f"File '{file_path}' uploaded as '{object_name}' to bucket '{bucket_name}'.")
# 列出存储桶中的所有对象
objects = minio_client.list_objects(bucket_name, recursive=True)
print(f"Objects in bucket '{bucket_name}':")
for obj in objects:
print(obj.object_name)
# 下载文件从存储桶
minio_client.fget_object(bucket_name, object_name, download_path)
print(f"File '{object_name}' downloaded from bucket '{bucket_name}' to '{download_path}'.")
# 删除存储桶中的对象
# 注释便于验证
# minio_client.remove_object(bucket_name, object_name)
# print(f"Object '{object_name}' deleted from bucket '{bucket_name}'.")
except S3Error as err:
print(err)
输出
Bucket 'bucket1' already exists.
File '../../data/file.txt' uploaded as 'example' to bucket 'bucket1'.
Objects in bucket 'bucket1':
example
kafka.png
File 'example' downloaded from bucket 'bucket1' to '../../data/file_download.txt'.
可能遇到的错误
- 访问密钥及安全密钥没配置正确
S3 operation failed; code: InvalidAccessKeyId, message: The Access Key Id you provided does not exist in our records., resource: /bucket1, request_id: 182E8D3B83F823D5, host_id: dd9025bab4ad464b049177c95eb6ebf374d3b3fd1af9251148b658df7ac2e3e8, bucket_name: bucket1
- endpoint 配置错误
minio.error.InvalidResponseError: non-XML response from server; Response code: 400, Content-Type: text/xml; charset=utf-8, Body: <?xml version="1.0" encoding="UTF-8"?><Error><Code>InvalidArgument</Code><Message>S3 API Requests must be made to API port.</Message><RequestId>0</RequestId></Error>