1 准备工作
1.创建目录
[root@k8s-storage tmp]# mkdir -pv minio/{data,conf}
mkdir: created directory ‘minio’
mkdir: created directory ‘minio/data’
mkdir: created directory ‘minio/conf’
[root@k8s-storage minio]# chmod 777 -R *
2.生成https证书
openssl req -x509 -newkey rsa:2048 \
-keyout minio.key \
-out minio.crt \
-days 36500 \
-nodes \
-subj "/CN=harbor.test.info" \
-addext "subjectAltName = DNS:harbor.test.info, DNS:test.com, IP:192.168.1.100, IP:12.3.3.4"
[root@k8s-storage minio]# ls
conf data minio.crt minio.key
2 启动minio
容器的9090是web管理端口
容器的9000是数据传输端口
docker run -d \
--restart unless-stopped \
--name minio02 \
-p 9001:9000 \
-p 9091:9090 \
-v ./data:/data \
-v ./conf:/root/.minio \
-v ./minio.crt:/root/.minio/certs/public.crt \
-v ./minio.key:/root/.minio/certs/private.key \
-e MINIO_ROOT_USER=admin \
-e MINIO_ROOT_PASSWORD=Wzy666@123 \
minio/minio server /data --console-address ":9090"
[root@k8s-storage minio]# docker ps -a | grep minio02
66a187075694 minio/minio "/usr/bin/docker-ent…" 13 seconds ago Up 13 seconds 0.0.0.0:9001->9000/tcp, :::9001->9000/tcp, 0.0.0.0:9091->9090/tcp, :::9091->9090/tcp minio02
3 设置bucket和密钥
访问web管理控制台
创建bucket
设置访问凭证
4 python代码测试
from minio import Minio
from minio.error import S3Error, InvalidResponseError, ServerError
import urllib3
# 配置信息
endpoint = "192.168.12.219:9001"
access_key = "MAyial2kEzrxGtm5SCYa"
secret_key = "v4j2AQZGsqHBcJTO3ei42upopMQG7f7wpS5ji5sU"
bucket_name = "gitlab-runner-cache-maven"
object_name = "test.txt" # 存储在 MinIO 中的对象名称
local_upload_file = "./python-file/upload-python.txt"
local_download_file = "./python-file/downloaded-python.txt"
# 创建自定义HTTP客户端(禁用SSL验证)
http_client = urllib3.PoolManager(
cert_reqs='CERT_NONE', # 禁用SSL证书验证
assert_hostname=False
)
# 创建 MinIO 客户端(使用HTTPS协议并禁用SSL验证)
client = Minio(
endpoint,
access_key=access_key,
secret_key=secret_key,
secure=True, # 使用HTTPS协议
http_client=http_client # 自定义HTTP客户端
)
def upload_file():
try:
# 确保 bucket 存在
found = client.bucket_exists(bucket_name)
if not found:
print(f"Bucket '{bucket_name}' 不存在")
return
# 上传文件
client.fput_object(bucket_name, object_name, local_upload_file)
print(f"✅ 上传成功: {local_upload_file} → {bucket_name}/{object_name}")
except (S3Error, InvalidResponseError, ServerError) as err:
print(f"❌ 上传失败: {err}")
raise
def download_file():
try:
# 下载文件
client.fget_object(bucket_name, object_name, local_download_file)
print(f"✅ 下载成功: {bucket_name}/{object_name} → {local_download_file}")
except (S3Error, InvalidResponseError, ServerError) as err:
print(f"❌ 下载失败: {err}")
raise
if __name__ == "__main__":
try:
upload_file()
download_file()
except Exception as e:
print(f"操作过程中发生致命错误: {e}")