Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a directory to Minio using Python

Tags:

minio

Using the Python client, how can I recursively upload all files in a local directory to Minio while preserving the directory structure?

like image 575
Emre Sülün Avatar asked Oct 20 '25 14:10

Emre Sülün


1 Answers

This recursive function uploads all files presuming Minio client is initialized beforehand:

import glob

def upload_local_directory_to_minio(local_path, bucket_name, minio_path):
    assert os.path.isdir(local_path)

    for local_file in glob.glob(local_path + '/**'):
        local_file = local_file.replace(os.sep, "/") # Replace \ with / on Windows
        if not os.path.isfile(local_file):
            upload_local_directory_to_minio(
                local_file, bucket_name, minio_path + "/" + os.path.basename(local_file))
        else:
            remote_path = os.path.join(
                minio_path, local_file[1 + len(local_path):])
            remote_path = remote_path.replace(
                os.sep, "/")  # Replace \ with / on Windows
            client.fput_object(bucket_name, remote_path, local_file)

Adapted from https://stackoverflow.com/a/56870303/5964489

like image 169
Emre Sülün Avatar answered Oct 22 '25 05:10

Emre Sülün



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!