python minio

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from minio import Minio
 
file_name = '3e09ca66d9444906935b0171e26891f1.mp4'
file_path = r'E:\集成资料\视频素材'
barrel = "testdata"
 
 
def upload_file():
    # 创建minio客户端
    client = Minio(endpoint="xxx.xxx.xxx.xxx:xxxxx",
                   access_key='xxxxx',
                   secret_key='xxxxx',
                   secure=False  # 使用http
                   )
    # 创建桶
    client.make_bucket(bucket_name=barrel)
 
    # 删除桶
    client.remove_bucket(barrel)
 
    # 获取桶列表
    barrel_list = client.list_buckets()
    print(barrel_list)
 
    # 获取桶中的数据信息,不查子文件夹中的数据
    bucket_objects = client.list_objects(barrel)
    for bucket_object in bucket_objects:
        print(bucket_object.object_name)
 
    # 列出名称以1-4开头的数据信息
    bucket_objects = client.list_objects(barrel, prefix="1-4")
    for bucket_object in bucket_objects:
        print(bucket_object)
 
    # 递归遍历桶中的数据信息,读取子文件夹下的文件
    bucket_objects = client.list_objects(barrel, recursive=True)
    for bucket_object in bucket_objects:
        print(bucket_object.object_name)
 
    # 递归查找以/data开头的数据信息
    data = list()
    for root in ["/data1"]:
        bucket_objects = client.list_objects(barrel, prefix=root, recursive=True)
        for bucket_object in bucket_objects:
            data.append(bucket_object.object_name)
            # print(bucket_object.object_name)
    print(len(data))
 
    # 递归查找以data1同一层级的数据信息
    bucket_objects = client.list_objects(barrel, recursive=True, start_after="data1")
    for bucket_object in bucket_objects:
        print(bucket_object.object_name)
 
    # 上传文件, bucket_name: 桶名称, object_name:上传到桶中完整的文件路径, file_path:文件本地所在完整路径
    result = client.fput_object(bucket_name=barrel, object_name="data1/" + file_name,
    file_path=file_path + "/" + file_name)
    print(result.object_name, result.bucket_name, result.etag)
 
    # 下载文件,bucket_name: 桶名称, object_name:被下载文件完整路径, file_path:保存到本地的完整路径
    result = client.fget_object(bucket_name=barrel, object_name="data1/60719d5c50e833d4fa8af3b7412d40000a2.jpg",
                                file_path=r"E:\集成资料\测试项目\minio\1.jpg")
    print(result.object_name, result.content_type, result.owner_name)
 
    # 判断桶是否存在
    check_bucket = client.bucket_exists(barrel)
    if not check_bucket:  # 不存在则创建桶
        client.make_bucket(barrel)
 
 
if __name__ == '__main__':
    upload_file()

 

posted @   乔小生1221  阅读(497)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-04-19 用脚本来运行scrapy crawl ...
2019-04-19 生成器的两种方式
点击右上角即可分享
微信分享提示