|
| 1 | +from faasmctl.util.config import get_faasm_ini_file, get_faasm_ini_value |
| 2 | +from minio import Minio |
| 3 | +from minio.error import S3Error |
| 4 | +from os import listdir |
| 5 | +from os.path import isfile, join |
| 6 | + |
| 7 | + |
| 8 | +def get_minio_client(): |
| 9 | + minio_port = get_faasm_ini_value(get_faasm_ini_file(), "Faasm", "minio_port") |
| 10 | + |
| 11 | + client = Minio( |
| 12 | + "localhost:{}".format(minio_port), |
| 13 | + access_key="minio", |
| 14 | + secret_key="minio123", |
| 15 | + secure=False, |
| 16 | + region="", |
| 17 | + ) |
| 18 | + |
| 19 | + return client |
| 20 | + |
| 21 | + |
| 22 | +def list_buckets(): |
| 23 | + client = get_minio_client() |
| 24 | + for bucket in client.list_buckets(): |
| 25 | + print(bucket) |
| 26 | + |
| 27 | + |
| 28 | +def list_objects(bucket, recursive=False): |
| 29 | + client = get_minio_client() |
| 30 | + for bucket_key in client.list_objects(bucket, recursive=recursive): |
| 31 | + print(bucket_key.object_name) |
| 32 | + |
| 33 | + |
| 34 | +def clear_bucket(bucket): |
| 35 | + client = get_minio_client() |
| 36 | + |
| 37 | + # Got to make sure the bucket is empty first |
| 38 | + for bucket_key in client.list_objects(bucket, recursive=True): |
| 39 | + client.remove_object(bucket, bucket_key.object_name) |
| 40 | + |
| 41 | + client.remove_bucket(bucket) |
| 42 | + |
| 43 | + |
| 44 | +def upload_file(bucket, host_path, s3_path): |
| 45 | + client = get_minio_client() |
| 46 | + |
| 47 | + # Create the bucket if it does not exist |
| 48 | + found = client.bucket_exists(bucket) |
| 49 | + if not found: |
| 50 | + client.make_bucket(bucket) |
| 51 | + |
| 52 | + # Upload the file, renaming it in the process |
| 53 | + try: |
| 54 | + client.fput_object(bucket, s3_path, host_path) |
| 55 | + except S3Error as ex: |
| 56 | + print("error: error uploading file to s3: {}".format(ex)) |
| 57 | + raise RuntimeError("error: error uploading file to s3") |
| 58 | + |
| 59 | + |
| 60 | +def upload_dir(bucket, host_path, s3_path): |
| 61 | + for f in listdir(host_path): |
| 62 | + host_file_path = join(host_path, f) |
| 63 | + |
| 64 | + if isfile(host_file_path): |
| 65 | + s3_file_path = join(s3_path, f) |
| 66 | + upload_file(bucket, host_file_path, s3_file_path) |
| 67 | + |
| 68 | + |
| 69 | +def dump_object(bucket, path): |
| 70 | + client = get_minio_client() |
| 71 | + response = client.get_object(bucket, path) |
| 72 | + |
| 73 | + return response |
0 commit comments