前言
python版本:3.7
minio版本:5.0.5
1 | # 使用pip安装 |
参考文档:
快速入门:https://docs.min.io/cn/python-client-quickstart-guide.html
完整文档:https://docs.min.io/cn/python-client-api-reference.html
API示例:https://github.com/minio/minio-py/tree/master/examples
初始化MinIO Client
1 | from minio import Minio |
操作存储桶
创建一个存储桶
make_bucket(bucket_name, location=’us-east-1’) -> None
1 | try: |
列出所有的存储桶
list_buckets() -> list
1 | try: |
检查存储桶是否存在
bucket_exists(bucket_name) -> bool
1 | try: |
删除存储桶
remove_bucket(bucket_name) -> None
1 | try: |
列出存储桶中所有对象
list_objects(bucket_name, prefix=None, recursive=False) -> Iterator
1 | try: |
列出存储桶中未完整上传的对象
list_incomplete_uploads(bucket_name, prefix, recursive=False) -> Iterator
1 | try: |
获取存储桶的当前策略
get_bucket_policy(bucket_name, prefix) -> Policy枚举
1 | # Get current policy of all object paths in bucket that begin with my-prefixname. |
给指定的存储桶设置存储桶策略
set_bucket_policy(bucket_name, prefix, policy) -> Policy枚举
1 | # Set policy Policy.READ_ONLY to all object paths in bucket that begin with my-prefixname. |
获取存储桶上的通知配置
get_bucket_notification(bucket_name) -> dict
1 | # Get the notifications configuration for a bucket. |
给存储桶设置通知配置
set_bucket_notification(bucket_name, notification) ->
删除存储桶上配置的所有通知
remove_all_bucket_notification(bucket_name)
监听存储桶上的通知,可以额外提供前缀、后缀和时间类型来进行过滤
listen_bucket_notification(bucket_name, prefix, suffix, events)
1 | # Put a file with default content-type. |
操作对象
下载一个对象
get_object(bucket_name, object_name, request_headers=None)
1 | # Get a full object. |
下载一个对象的指定区间的字节数组
get_partial_object(bucket_name, object_name, offset=0, length=0, request_headers=None)
1 | # Offset the download by 2 bytes and retrieve a total of 4 bytes. |
下载并将文件保存到本地
get_object(bucket_name, object_name, file_path, request_headers=None)
1 | # Get a full object and prints the original object stat information. |
拷贝对象存储服务上的源对象到一个新对象
copy_object(bucket_name, object_name, object_source, copy_conditions=None, metadata=None)
1 | import time |
添加一个新的对象到对象存储服务
put_object(bucket_name, object_name, data, length, content_type=’application/octet-stream’, metadata=None)
1 | import os |
通过文件上传到对象中
fput_object(bucket_name, object_name, file_path, content_type=’application/octet-stream’, metadata=None)
1 | # Put an object 'myobject' with contents from '/tmp/otherobject', upon success prints the etag identifier computed by server. |
获取对象的元数据
stat_object(bucket_name, object_name)
1 | # Fetch stats on your object. |
删除一个对象
remove_object(bucket_name, object_name)
1 | # Remove an object. |
删除存储桶中的多个对象
remove_objects(bucket_name, objects_iter)
1 | # Remove multiple objects in a single library call. |
删除一个未完整上传的对象
remove_incomplete_upload(bucket_name, object_name)
1 | # Remove an partially uploaded object. |
Presigned操作
生成一个用于HTTP GET操作的presigned URL
presigned_get_object(bucket_name, object_name, expiry=timedelta(days=7))
1 | from datetime import timedelta |
生成一个用于HTTP PUT操作的presigned URL
presigned_put_object(bucket_name, object_name, expires=timedelta(days=7))
1 | from datetime import timedelta |
允许给POST操作的presigned URL设置策略条件
presigned_post_policy(PostPolicy)