import io.minio.MinioClient;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class MinioUtil {
private static String MINIO_ACCESS_KEY = "你的MinIO访问密钥";
private static String MINIO_SECRET_KEY = "你的MinIO密钥";
private static String MINIO_URL = "你的MinIO服务器地址";
private MinioClient minioClient;
public MinioUtil() throws InvalidKeyException, IOException, NoSuchAlgorithmException, MinioException {
minioClient = new MinioClient(MINIO_URL, MINIO_ACCESS_KEY, MINIO_SECRET_KEY);
}
// 检查存储桶是否存在
public boolean bucketExists(String bucketName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
boolean isExist = minioClient.bucketExists(bucketName);
return isExist;
}
// 创建存储桶
public boolean makeBucket(String bucketName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
boolean isExist = minioClient.bucketExists(bucketName);
if(isExist) {
throw new RuntimeException("Bucket already exists");
}
minioClient.makeBucket(bucketName);
return true;
}
// 列出所有存储桶
public List<Bucket> listBuckets() throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
List<Bucket> buckets = minioClient.listBuckets();
return buckets;
}
// 列出存储桶中的对象
public Iterable<Result<Item>> listObjects(String bucketName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
Iterable<Result<Item>> items = minioClient.listObjects(bucketName);
return items;
}
// 上传对象
public void uploadObject(String bucketName, String objectName, String filePath) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
minioClient.putObject(bucketName, objectName, filePath);
}
// 下载对象
public void downloadObject(String bucketName, String objectName, String downloadPath) throws IOException, NoSuchAlgorithmException, InvalidKeyException, MinioException {
minioClient.getObject(bucketName, objectName, downloadPath);
}
// 删除对象
public void removeObject(String bucketName, String objectName) thr
评论已关闭