Minio集成工具类

 填好aksk,endpoint后运行


import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;

import java.util.List;

public class MinioUtil {
    private static final String ACCESS_KEY = "";
    private static final String SECRET_KEY = "";
    private static final String ENDPOINT = "";

    private MinioClient minioClient;

    public MinioUtil() {
        minioClient = MinioClient.builder()
                .endpoint(ENDPOINT)
                .credentials(ACCESS_KEY, SECRET_KEY)
                .build();
    }

    /**
     * 创建一个桶
     * @param bucketName
     * @throws MinioException
     */
    public void createBucket(String bucketName) throws MinioException {
        try {
            boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!isExist) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                System.out.println("Bucket created successfully: " + bucketName);
            } else {
                System.out.println("Bucket already exists: " + bucketName);
            }
        } catch (Exception e) {
            throw new MinioException("Error creating bucket: " + bucketName, e.getMessage());
        }
    }

    /**
     * 向桶名上传一个对象,本地数据路径存放在filePath下
     * @param bucketName 桶名
     * @param objectName 对象名称
     * @param filePath 本地数据路径
     * @throws MinioException
     */
    public void uploadFile(String bucketName, String objectName, String filePath) throws MinioException {
        try {
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .filename(filePath)
                            .build()
            );
            System.out.println("File uploaded successfully: " + objectName);
        } catch (Exception e) {
            throw new MinioException("Error uploading file: " + objectName, e.getMessage());
        }
    }

    /**
     * 从bucketName下载一个文件,存放在本地filePath下
     * @param bucketName 桶名
     * @param objectName 文件名
     * @param filePath 存放路径
     * @throws MinioException
     */
    public void downloadFile(String bucketName, String objectName, String filePath) throws MinioException {
        try {
            minioClient.downloadObject(
                    DownloadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .filename(filePath)
                            .build()
            );
            System.out.println("File downloaded successfully: " + objectName);
        } catch (Exception e) {
            throw new MinioException("Error downloading file: " + objectName, e.getMessage());
        }
    }

    /**
     * 列出所有桶名
     * @throws MinioException
     */
    public void listBuckets() throws MinioException {
        try {
            List buckets = minioClient.listBuckets();
            for (Bucket bucket : buckets) {
                System.out.println("Bucket: " + bucket.name() + ", Creation Date: " + bucket.creationDate());
            }
        } catch (Exception e) {
            throw new MinioException("Error listing buckets", e.getMessage());
        }
    }

    /**
     * 列出桶名下所有对象
     * @param bucketName
     * @throws MinioException
     */
    public void listObjects(String bucketName) throws MinioException {
        try {
            Iterable> objects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
            for (Result result : objects) {
                Item item = result.get();
                System.out.println("Object: " + item.objectName() + ", Size: " + item.size() + ", Last Modified: " + item.lastModified());
            }
        } catch (Exception e) {
            throw new MinioException("Error listing objects in bucket: " + bucketName, e.getMessage());
        }
    }

    /**
     * 删除桶中一个对象
     * @param bucketName 桶名
     * @param objectName 对象名称
     * @throws MinioException
     */
    public void deleteObject(String bucketName, String objectName) throws MinioException {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
            System.out.println("Object deleted successfully: " + objectName);
        } catch (Exception e) {
            throw new MinioException("Error deleting object: " + objectName, e.getMessage());
        }
    }

    /**
     * 删除桶
     * @param bucketName
     * @throws MinioException
     */
    public void deleteBucket(String bucketName) throws MinioException {
        try {
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
            System.out.println("Bucket deleted successfully: " + bucketName);
        } catch (Exception e) {
            throw new MinioException("Error deleting bucket: " + bucketName, e.getMessage());
        }
    }

    public static void main(String[] args) {
        try {
            MinioUtil minioUtil = new MinioUtil();
            minioUtil.createBucket("minio-0");
//            minioUtil.uploadFile(
//                    "minio-0",
//                    "screenshot-20230822-141023.png",
//                    "C:\\Users\\pei\\Desktop\\screenshot-20230822-141023.png");
            minioUtil.downloadFile(
                    "minio-0",
                    "minio-0",
                    "C:\\Users\\pei\\Desktop\\test-minio.png");
            minioUtil.listBuckets();
            minioUtil.listObjects("minio-0");
            minioUtil.deleteObject("minio-0", "my-object");
            minioUtil.deleteBucket("minio-0");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java,minio,对象存储)