Minio云存储平台文件上传python脚本示例


最近的项目踩坑,留此记录以备后用。

import boto3
from botocore.exceptions import NoCredentialsError

# MinIO服务器的配置信息
minio_endpoint = '地址信息'  # 例如 'localhost:9000'
access_key = 'access_key信息'
secret_key = 'secret_key信息'
bucket_name = 'nmsjpt'
file_path = 'D:\\minio\\doc11-test.docx'  # 本地文件路径
object_name = 'uploadFile/150622001234/ReportForm/1506220012340000001/doc11-test.docx'  # 上传到MinIO后的文件名

# 创建一个boto3客户端
s3_client = boto3.client(
    's3',
    endpoint_url=f'http://{minio_endpoint}',
    aws_access_key_id=access_key,
    aws_secret_access_key=secret_key
)

try:
    # 上传文件
    s3_client.upload_file(file_path, bucket_name, object_name)
    print(f'文件 {file_path} 成功上传到 {bucket_name}/{object_name}')
except FileNotFoundError:
    print(f'文件 {file_path} 未找到')
except NoCredentialsError:
    print('凭证错误,请检查你的 Access Key 和 Secret Key')
except Exception as e:
    print(f'上传文件时发生错误: {e}')
正文完

搜索