ubuntu22 安装和启动minio 并用Python上传下载文件

 安装和启动部分参考了这个:Ubuntu 22.04 安装 MinIO_ubuntu安装minio-CSDN博客

# 下载
wget https://dl.min.io/server/minio/release/linux-amd64/minio
# 赋予执行权限
chmod +x minio
# 放入统一管理的文件夹(非必选)
[ -d ~/softwares ] || mkdir ~/softwares
mv minio ~/softwares
cd ~/softwares
# 软连接到系统目录
sudo ln -s `pwd`/minio /usr/local/bin/minio
minio --version

再就是添加用户和配置服务:

sudo adduser --disabled-password --no-create-home --gecos "" minio-user
sudo mkdir /data
sudo chown -R minio-user:minio-user /data

设置用户名、密码、端口等环境变量

echo '
# 指定数据存储目录(注意:这个目录要存在且拥有相对应的权限)
MINIO_VOLUMES="/data"

# 监听端口
MINIO_OPTS="--address :9000 --console-address :9099"

# 老版本使用MINIO_ACCESS_KEY/MINIO_SECRET_KEY,新版本已不建议使用
# Access key (账号)
# MINIO_ACCESS_KEY="minioadmin"
# Secret key (密码)
# MINIO_SECRET_KEY="minioadmin"

# 新版本使用;指定默认的用户名和密码,其中用户名必须大于3个字母,否则不能启动
MINIO_ROOT_USER="mini"
MINIO_ROOT_PASSWORD="MinioAdmin123"

# 区域值,标准格式是“国家-区域-编号”,
#MINIO_REGION="cn-north-1" # 启用这行会导致API无法新建存储桶

# 域名
# MINIO_DOMAIN=minio.your_domain.com
' > minio.conf
sudo mv minio.conf /etc/default/minio.conf

配置service文件

echo '
[Unit]
Description=MinIO
Documentation=https://docs.min.io
Wants=network-online.target
After=network-online.target
AssertFileIsExecutable=/usr/local/bin/minio
[Service]
WorkingDirectory=/usr/local/

ProtectProc=invisible

# 指向3.1节中的配置文件
EnvironmentFile=/etc/default/minio

ExecStartPre=/bin/bash -c "if [ -z \"${MINIO_VOLUMES}\" ]; then echo \"Variable MINIO_VOLUMES not set in /etc/default/minio\"; exit 1; fi"
ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES

# Let systemd restart this service always
Restart=always

# Specifies the maximum (1M) file descriptor number that can be opened by this process
LimitNOFILE=1048576

# Specifies the maximum number of threads this process can create
TasksMax=infinity

# Disable timeout logic and wait until process is stopped
TimeoutStopSec=infinity
SendSIGKILL=no

[Install]
WantedBy=multi-user.target
Alias=minio.service
' > minio.service
sudo mv minio.service /usr/lib/systemd/system/

启动服务并设为开机自启动

# 重新加载服务配置文件,使服务生效
sudo systemctl daemon-reload

# 将服务设置为开机启动
sudo systemctl enable minio

# 服务立即启动
sudo systemctl start minio

# 查看minio服务当前状态
sudo systemctl status minio

在浏览器打开http://your-server-ip:9099就能看到minio的管理页面了

创建客户端用的access keys 

点击左侧Access Keys,在新页面中点击Create access key,进入下一个页面后,点Create

上传测试脚本

import os
from pathlib import Path

from minio import Minio # pip install minio
from minio.error import S3Error

HOST = os.getenv("MINIO_HOST", "127.0.0.1:9000")  # 服务器IP
# 管理页面上创建的
ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY") or "5ksdYTsomGeBBZSEUtT0"
SECRET_KEY = os.getenv("MINIO_SECRET_KEY") or "x9x5q0iacgr6bNRTtLhJXgJLWsW4Qg6MhrnqyKOG"


def main():
    # Create a client with the MinIO server playground, its access key
    # and secret key.
    print(f"{HOST = }; {ACCESS_KEY=}; {SECRET_KEY=}")
    client = Minio(HOST, access_key=ACCESS_KEY, secret_key=SECRET_KEY, secure=False)

    # The file to upload, change this path if needed
    source_file = "/tmp/test-file.txt"  # 要上传的文件
    if not Path(source_file).exists():
        raise FileNotFoundError(source_file)

    # The destination bucket and filename on the MinIO server
    bucket_name = "cn-north-1"
    destination_file = "my-test-file.txt"  # 上传成功后,要展示的名称

    # Make the bucket if it doesn't exist.
    found = client.bucket_exists(bucket_name)
    if not found:
        client.make_bucket(bucket_name)
        print("Created bucket", bucket_name)
    else:
        print("Bucket", bucket_name, "already exists")

    # Upload the file, renaming it in the process
    client.fput_object(
        bucket_name,
        destination_file,
        source_file,
    )
    print(
        source_file,
        "successfully uploaded as object",
        destination_file,
        "to bucket",
        bucket_name,
    )


if __name__ == "__main__":
    try:
        main()
    except S3Error as exc:
        print("error occurred.", exc)

测试效果:

Bucket cn-north-1 already exists
/tmp/test-file.txt successfully uploaded as object my-test-file.txt to bucket cn-north-1

 ### 使用Python SDK将远程文件下载到本地

# download_file.py
import os
from pathlib import Path

from minio import Minio
from minio.error import S3Error

HOST = os.getenv("MINIO_HOST", "127.0.0.1:9000")  # 服务器IP
# 管理页面上创建的
ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY") or "5ksdYTsomGeBBZSEUtT0"
SECRET_KEY = os.getenv("MINIO_SECRET_KEY") or "x9x5q0iacgr6bNRTtLhJXgJLWsW4Qg6MhrnqyKOG"


def main():
    # Create a client with the MinIO server playground, its access key
    # and secret key.
    client = Minio(HOST, access_key=ACCESS_KEY, secret_key=SECRET_KEY, secure=False)

    bucket_name = "cn-north-1"
    remote_file = "my-test-file.txt"  # 要下载的文件

    try:
        response = client.get_object(bucket_name, remote_file)
    except S3Error as exc:
        print("Failed to download:", exc)
    else:
        content: bytes = response.read()
        p = Path() / Path(remote_file).name
        size = p.write_bytes(content)
        print(f"Success to download {remote_file} with {size=} and save to local: {p}")
    finally:
        response.close()
        response.release_conn()


if __name__ == "__main__":
    main()

执行结果:

Success to download my-test-file.txt with size=2451 and save to local: my-test-file.txt

更多Python交互示例见:

https://min.io/docs/minio/linux/developers/python/API.html

https://github.com/minio/minio-py/tree/master/examples

附:Nginx配置示例

server {
 server_name minio.waketzheng.top;

 # To allow special characters in headers
 ignore_invalid_headers off;
 # Allow any size file to be uploaded.
 # Set to a value such as 1000m; to restrict file size to a specific value
 client_max_body_size 0;
 # To disable buffering
 proxy_buffering off;

 location / {
   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 Host $http_host;

   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://localhost:9000; # If you are using docker-compose this would be the hostname i.e. minio
   # Health Check endpoint might go here. See https://www.nginx.com/resources/wiki/modules/healthcheck/
   # /minio/health/live;
 }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/minio.waketzheng.top/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/minio.waketzheng.top/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = minio.waketzheng.top) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


 listen 80;
 server_name minio.waketzheng.top;
    return 404; # managed by Certbot


}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值