文章目录


​一、python对接MinIO​



一、python对接MinIO

1.首先执行命令安装包

pip install minio

2.创建客户端

from minio import Minio

# 创建具有匿名访问权限的客户端。
client = Minio("play.min.io")

# 创建具有访问权限和密钥的客户端。
client = Minio("s3.amazonaws.com", "ACCESS-KEY", "SECRET-KEY")

# 创建具有特定区域的访问密钥和密钥的客户端。
client = Minio(
"play.minio.io:9000",
access_key="Q3AM3UQ867SPQQA43P2F",
secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
region="my-region",
)

# 使用代理服务器创建带有自定义HTTP客户端的客户端。
import urllib3
client = Minio(
"SERVER:PORT",
access_key="ACCESS_KEY",
secret_key="SECRET_KEY",
secure=True,
http_client=urllib3.ProxyManager(
"https://PROXYSERVER:PROXYPORT/",
timeout=urllib3.Timeout.DEFAULT_TIMEOUT,
cert_reqs="CERT_REQUIRED",
retries=urllib3.Retry(
total=5,
backoff_factor=0.2,
status_forcelist=[500, 502, 503, 504],
),
),
)

3.桶操作

3.1 创建桶

# 创建bucket。
client.make_bucket("my-bucket")

# 在特定区域创建bucket。
client.make_bucket("my-bucket", "us-west-1")

# 在特定区域创建具有对象锁定功能的桶。
client.make_bucket("my-bucket", "eu-west-2", object_lock=True)

3.2 查询桶

3.2.1 查询桶和判断桶是否存在

# 查询所有桶
buckets = client.list_buckets()
for bucket in buckets:
print(bucket.name, bucket.creation_date)
# 判断桶是否存在
if client.bucket_exists("my-bucket"):
print("my-bucket exists")
else:
print("my-bucket does not exist")

3.2.2 列出桶的对象信息

# 列出对象信息。
objects = client.list_objects("my-bucket")
for obj in objects:
print(obj)

# 列出名称以“my/prefix/”开头的对象信息。
objects = client.list_objects("my-bucket", prefix="my/prefix/")
for obj in objects:
print(obj)

# 递归地列出对象信息。
objects = client.list_objects("my-bucket", recursive=True)
for obj in objects:
print(obj)

# 递归列出名称以开头的对象信息
# "my/prefix/".
objects = client.list_objects(
"my-bucket", prefix="my/prefix/", recursive=True,
)
for obj in objects:
print(obj)

# 在对象名称后递归列出对象信息
objects = client.list_objects(
"my-bucket", recursive=True, start_after="my/prefix/world/1",
)
for obj in objects:
print(obj)

3.3 删除桶

client.remove_bucket("my-bucket")

3.4 桶的策略配置

3.4.1 策略查询

policy = client.get_bucket_policy("my-bucket")

3.4.2 策略设置

# 匿名只读存储桶策略。
policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": ["s3:GetBucketLocation", "s3:ListBucket"],
"Resource": "arn:aws:s3:::my-bucket",
},
{
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-bucket/*",
},
],
}
client.set_bucket_policy("my-bucket", json.dumps(policy))

# 匿名读写存储桶策略。
policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": [
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
],
"Resource": "arn:aws:s3:::my-bucket",
},
{
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListMultipartUploadParts",
"s3:AbortMultipartUpload",
],
"Resource": "arn:aws:s3:::my-bucket/images/*",
},
],
}
client.set_bucket_policy("my-bucket", json.dumps(policy))

3.4.3 策略删除

client.delete_bucket_policy("my-bucket")

3.5 桶的通知配置

3.5.1 桶的通知配置

config = client.get_bucket_notification("my-bucket")

3.5.2 桶的通知设置

config = NotificationConfig(
queue_config_list=[
QueueConfig(
"QUEUE-ARN-OF-THIS-BUCKET",
["s3:ObjectCreated:*"],
config_id="1",
prefix_filter_rule=PrefixFilterRule("abc"),
),
],
)
client.set_bucket_notification("my-bucket", config)

3.5.3 桶的通知删除

client.delete_bucket_notification("my-bucket")

3.6 桶的前缀和后缀事件

with client.listen_bucket_notification(
"my-bucket",
prefix="my-prefix/",
events=["s3:ObjectCreated:*", "s3:ObjectRemoved:*"],
) as events:
for event in events:
print(event)

3.7 桶的加密配置

3.7.1 加密查询

config = client.get_bucket_encryption("my-bucket")

3.7.2 加密设置

client.set_bucket_encryption(
"my-bucket", SSEConfig(Rule.new_sse_s3_rule()),
)

3.7.3 加密删除

client.delete_bucket_encryption("my-bucket")

3.8 桶的版本控制配置

3.8.1 版本控制查询

config = client.get_bucket_versioning("my-bucket")
print(config.status)

3.8.2 版本控制设置

client.set_bucket_versioning("my-bucket", VersioningConfig(ENABLED))

3.9 桶的复制配置

3.9.1 复制查询

config = client.get_bucket_replication("my-bucket")

3.9.2 复制设置

config = ReplicationConfig(
"REPLACE-WITH-ACTUAL-ROLE",
[
Rule(
Destination(
"REPLACE-WITH-ACTUAL-DESTINATION-BUCKET-ARN",
),
ENABLED,
delete_marker_replication=DeleteMarkerReplication(
DISABLED,
),
rule_filter=Filter(
AndOperator(
"TaxDocs",
{"key1": "value1", "key2": "value2"},
),
),
rule_id="rule1",
priority=1,
),
],
)
client.set_bucket_replication("my-bucket", config)

3.9.3 复制删除

client.delete_bucket_replication("my-bucket")

3.10 桶的生命周期

3.10.1 生命周期查询

config = client.get_bucket_lifecycle("my-bucket")

3.10.2 生命周期设置

config = LifecycleConfig(
[
Rule(
ENABLED,
rule_filter=Filter(prefix="documents/"),
rule_id="rule1",
transition=Transition(days=30, storage_class="GLACIER"),
),
Rule(
ENABLED,
rule_filter=Filter(prefix="logs/"),
rule_id="rule2",
expiration=Expiration(days=365),
),
],
)
client.set_bucket_lifecycle("my-bucket", config)

3.10.3 生命周期删除

client.delete_bucket_lifecycle("my-bucket")

3.11 桶的标签

3.11.1 标签查询

tags = client.get_bucket_tags("my-bucket")

3.11.2 标签设置

tags = Tags.new_bucket_tags()
tags["Project"] = "Project One"
tags["User"] = "jsmith"
client.set_bucket_tags("my-bucket", tags)

3.11.3 标签删除

client.delete_bucket_tags("my-bucket")

3.12 桶的对象锁

3.12.1 对象锁查询

config = client.get_object_lock_config("my-bucket")

3.12.2 对象锁设置

config = ObjectLockConfig(GOVERNANCE, 15, DAYS)
client.set_object_lock_condig("my-bucket", config)

3.12.3 对象锁删除

client.delete_object_lock_config("my-bucket")

总结


  • 客户端:用于连接MinIO文件存储服务器
  • 桶:里面存储着对象