# 导入MinIO Python客户端库
from minio import Minio
from minio.error import S3Error
# 初始化MinIO客户端
client = Minio(
"play.min.io", # MinIO服务器地址
access_key="YOUR-ACCESSKEYID", # 访问密钥
secret_key="YOUR-SECRETACCESSKEY", # 秘密密钥
secure=True # 是否使用HTTPS
)
def upload_file(bucket_name, object_name, file_path):
try:
# 检查存储桶是否存在,如果不存在则创建
found = client.bucket_exists(bucket_name)
if not found:
client.make_bucket(bucket_name)
else:
print(f"Bucket '{bucket_name}' already exists")
# 上传文件到指定的存储桶
client.fput_object(bucket_name, object_name, file_path)
print(f"File '{file_path}' uploaded successfully as '{object_name}' to bucket '{bucket_name}'")
except S3Error as err:
print(f"Error occurred: {err}")
# 示例调用
upload_file("my-bucket", "my-object", "/path/to/myfile.txt")
minio
库,该库提供了与MinIO服务器交互的功能。secure=True
表示使用HTTPS连接。upload_file
函数用于将本地文件上传到MinIO服务器上的指定存储桶中。fput_object
方法将文件上传到指定的存储桶中,并打印成功信息。如果你没有安装minio
库,可以通过以下命令安装:
pip install minio
上一篇:python线上编译器
下一篇:python中的集合
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站