Python对象存储实践-minio库
MinIO Python Client SDK提供通过API访问Minio对象存储或者其他Amazon S3兼容的服务。
1. 安装Minio Python SDK
minio python SDK 需要python版本在3.7+,可以通过pip或者源码安装:
pip安装
pip3 install minio
源码安装
git clone https://github.com/minio/minio-py
cd minio-py
python setup.py install
2.代码示例
参考官方API文档:https://min.io/docs/minio/linux/developers/python/API.htm
这里选取几个常用的API结合本地的Minio环境进行代码展示,主要功能包括:
- 创建一个新的存储桶
- 打印桶列表
- 上传一个文件到存储桶
- 打印桶中的文件列表
- 下载桶中的一个文件到本地
- 删除桶中的一个文件
- 删除创建的桶
from minio import Minio
from minio.error import S3Error
# 创建一个s3客户端
client = Minio("192.168.0.158:9000", # 端口使用api端口,不是console端口
access_key="minioadmin",
secret_key="minioadmin",
secure=False, # http协议。如果是https,设置为true(缺省)。
# region="my-region", # 指定region
)
# 封装make_bucket()方法。创建桶。
def create_bucket(bucket_name):
try:
client.make_bucket(bucket_name)
print(f"Bucket {bucket_name} created successfully.")
except Exception as e:
print(f"Error creating bucket: {e}")
# 打印桶列表。
def list_buckets():
try:
buckets = client.list_buckets()
print("Existing buckets:")
for bucket in buckets:
print(bucket.name, bucket.creation_date)
except Exception as e:
print(f"Error listing buckets: {e}")
# 上传文件到桶。
def upload_file(bucket_name, object_name, file_name=None):
if object_name is None:
object_name = file_name
try:
client.fput_object(bucket_name, object_name, file_name)
print(f"File {file_name} uploaded to {bucket_name}/{object_name}.")
except FileNotFoundError:
print("The file was not found.")
except Exception as e:
print(f"Error uploading file: {e}")
# 打印桶中的文件列表。
def list_objects(bucket_name):
try:
objects = client.list_objects(bucket_name)
print(f"Objects in bucket {bucket_name}:")
for obj in objects:
print(obj.object_name)
except Exception as e:
print(f"Error listing objects: {e}")
# 从桶下载文件
def download_file(bucket_name, object_name, file_name):
try:
client.fget_object(bucket_name, object_name, file_name)
print(f"File {object_name} downloaded from {bucket_name} to {file_name}.")
except FileNotFoundError:
print("The file was not found.")
except Exception as e:
print(f"Error downloading file: {e}")
# 从桶删除文件
def delete_file(bucket_name, object_name):
try:
client.remove_object(bucket_name, object_name)
print(f"File {object_name} deleted from {bucket_name}.")
except Exception as e:
print(f"Error deleting file: {e}")
# 删除桶
def delete_bucket(bucket_name):
try:
client.remove_bucket(bucket_name)
print(f"Bucket {bucket_name} deleted successfully.")
except Exception as e:
print(f"Error deleting bucket: {e}")
def main():
# 列出所有桶
create_bucket('my-new-bucket')
list_buckets()
# 上传文件到桶
upload_file('my-new-bucket', 'file.txt', 'Practices/s3/file_to_upload.txt')
# 列出桶中的文件
list_objects('my-new-bucket')
# 从桶下载文件
download_file('my-new-bucket', 'file.txt', 'Practices/s3/file_downloaded.txt')
# 从桶删除文件
delete_file('my-new-bucket', 'file.txt')
# 删除桶
delete_bucket("my-new-bucket")
if __name__ == "__main__":
try:
main()
except S3Error as exc:
print("error occurred.", exc)