package com.yfd.platform.utils;

import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;

import java.io.FileOutputStream;
import java.io.InputStream;

public class MinioExample {

    public static void main(String[] args) throws Exception {
        // 连接到MinIO服务
        String endpoint = "http://192.168.1.7:21001";
        String accessKey = "minioadmin";
        String secretKey = "minioadmin";

        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();

        // 确保桶存在
        boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket("cloud-bucket").build());
        if (!isExist) {
            throw new RuntimeException("Bucket does not exist");
        }

        // 从这里开始获取图片
        // 获取图片
        String objectName = "your-image-name.jpg";
        try (InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
                .bucket("your-bucket-name")
                .object(objectName)
                .build());
             FileOutputStream fos = new FileOutputStream("path/to/save/image.jpg")) {

            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
        }

        System.out.println("Image downloaded successfully.");
    }
}
