minIo使用Python上传文件获取文件分享地址

1.安装对应的库 minio

pip install minio

 

2.上传文件

我的文件路径是:E:\\11aisource\\168.mp4

需要上传的路径是  air-video 这个bucket下的shuping文件夹

from minio import Minio
    # 本地搭建的地址,web端口默认9001,api端口默认9000
    endpoint = "192.168.3.22:9000"
    # access_key需要自己创建
    access_key = "xxxxxxx"
    # secret_key需要自己创建    
    secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    # 桶名,需要自己创建桶
    bucket_name = 'air-video'
    # 本地文件路径
    file = 'E:\\11aisource\\168.mp4'
    # 桶内存放路径,文件名可自定义防止重复
    object_name = 'shuping/168.mp4'
    # 创建minio对象
    client = Minio(endpoint, access_key=access_key, secret_key=secret_key, secure=False)
    
    # 上传文件
    with open(file, 'rb') as f:
        client.put_object(bucket_name, object_name, f, os.path.getsize(file))
    # 获取七天有效的文件链接
    res = client.get_presigned_url("GET", bucket_name, object_name, expires=timedelta(days=7))
    # res就是获取文件的url
    print(res)

 

3.补充:获取某个bucket下某个文件夹里面所有的文件内容

文件夹是 shuping/ 注意得加上/

r = client.list_objects(bucket_name, 'shuping/')
    for obj in r:
        print(obj.object_name)

 

posted @ 2024-12-15 02:44  lytcreate  阅读(620)  评论(0)    收藏  举报