直接用就OK import com.google.common.collect.Lists; import com.koala.koala_user.minio.bean.Fileinfo; import io.minio.*; import io.minio.errors.*; import io.minio.http.Method; import io.minio.messages.Bucket; import io.minio.messages.Item; import lombok.Data; import lombok.Getter; import lombok.Setter; import lombok.SneakyThrows; import okhttp3.OkHttpClient; import org.aspectj.lang.annotation.Before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.OkHttp3ClientHttpRequestFactory; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Random; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @Data @Configuration @ConfigurationProperties(prefix = "minio") public class MinioClientUtil { public String endpoint; public String accessKey; public String secretKey; public String bucketName; public static MinioClient minio; @Bean(name = {"minio"}) public void minioClient() { minio = MinioClient .builder() .endpoint(endpoint) .credentials(accessKey, secretKey) .build(); } /** * 检查存储桶是否存在 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public static boolean bucketExists(String bucketName) { return minio.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); } /** * 创建存储桶 * * @param bucketName 存储桶名称 */ @SneakyThrows public static boolean makeBucket(String bucketName) { boolean flag = bucketExists(bucketName); if (flag) { return true; } minio.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); return true; } /** * 列出所有存储桶名称 * * @return */ @SneakyThrows public static List<String> listBucketNames() { List<Bucket> list = listBuckets(); return list.stream().filter(Objects::nonNull).map(o -> o.name()).collect(Collectors.toList()); } /** * 列出所有存储桶 * * @return */ @SneakyThrows public static List<Bucket> listBuckets() { return minio.listBuckets(); } /** * 删除存储桶 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public static boolean removeBucket(String bucketName) { Iterable<Result<Item>> myObjects = listObjects(bucketName); for (Result<Item> result : myObjects) { Item item = result.get(); // 有对象文件,则删除失败 if (item.size() > 0) { return false; } } // 删除存储桶,注意,只有存储桶为空时才能删除成功。 minio.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); return !bucketExists(bucketName); } /** * 在test桶里创建文件夹对象 * * @param fileName */ public static void createFile(String fileName) { try { if (!bucketExists("test")) { //minio服务器创建桶 makeBucket("test"); } minio.putObject( PutObjectArgs.builder().bucket("test").object(fileName).stream( new ByteArrayInputStream(new byte[]{}), 0, -1) .build()); } catch (ErrorResponseException e) { e.printStackTrace(); } catch (InsufficientDataException e) { e.printStackTrace(); } catch (InternalException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (XmlParserException e) { e.printStackTrace(); } } /** * 列出存储桶中的所有对象 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public static Iterable<Result<Item>> listObjects(String bucketName) { return minio.listObjects(ListObjectsArgs.builder().bucket(bucketName).build()); } /** * 列出存储桶中的所有对象 * * @param bucketName 存储桶名称 * @return * @Info 2022-12-19 增加URL 直接拿到文件路径 */ @SneakyThrows public static List<Fileinfo> listFiles(String bucketName) { Iterable<Result<Item>> results = minio.listObjects( ListObjectsArgs.builder().bucket(bucketName).recursive(true).build()); List<Fileinfo> infos = new ArrayList<>(); results.forEach(r->{ Fileinfo info = new Fileinfo(); try { Item item = r.get(); info.setFilename(item.objectName()); info.setDirectory(item.isDir()); info.setUrl(getObjectURL(bucketName,info.getFilename())); infos.add(info); } catch (Exception e) { e.printStackTrace(); } }); return infos; } /** * 列出存储桶中的所有对象名称 * * @param bucketName 存储桶名称 * @return */ @SneakyThrows public static List<String> listObjectNames(String bucketName) { List<String> ret = Lists.newArrayList(); Iterable<Result<Item>> myObjects = listObjects(bucketName); for (Result<Item> result : myObjects) { Item item = result.get(); ret.add(item.objectName()); } return ret; } /** * 文件上传 * * @param bucketName * @param multipartFile */ @SneakyThrows public static Fileinfo putObject(String bucketName, MultipartFile multipartFile) { String fileName = multipartFile.getOriginalFilename(); String name = fileName; String prefix = ""; if (fileName.indexOf('.') != -1) { name = fileName.substring(0, fileName.indexOf('.')); prefix = fileName.substring(fileName.lastIndexOf(".")); } String newfileName = name+System.currentTimeMillis()+prefix; PutObjectArgs args = PutObjectArgs.builder() .bucket(bucketName) .object(newfileName) .contentType(multipartFile.getContentType()) .stream(multipartFile.getInputStream(), multipartFile.getSize(), -1) .build(); minio.putObject(args); Fileinfo fileinfo = new Fileinfo(); fileinfo.setUrl(getObjectURL(bucketName,newfileName)); fileinfo.setFilename(newfileName); return fileinfo; } /** * 通过InputStream上传对象 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @param in 要上传的流 * @param contentType 要上传的文件类型 MimeTypeUtils.IMAGE_JPEG_VALUE * @return */ @SneakyThrows public static ObjectWriteResponse putObject(String bucketName, String objectName, InputStream in, String contentType) { PutObjectArgs args = PutObjectArgs.builder() .bucket(bucketName) .object(objectName) .contentType(contentType) .stream(in, in.available(), -1) .build(); return minio.putObject(args); } /** * 以流的形式获取一个文件对象 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @return */ @SneakyThrows public static InputStream getObject(String bucketName, String objectName) { boolean flag = bucketExists(bucketName); if (!flag) { return null; } StatObjectResponse resp = statObject(bucketName, objectName); return resp == null ? null : minio.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build()); } /** * 获取对象的元数据 * * @param bucketName 存储桶名称 * @param objectName 存储桶里的对象名称 * @return */ @SneakyThrows public static StatObjectResponse statObject(String bucketName, String objectName) { return !bucketExists(bucketName) ? null : minio.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()); } /** * 删除文件 * * @param fileName * @return * @throws Exception */ @SneakyThrows public static void removeMinio(String fileName) { try { minio.removeObject(RemoveObjectArgs.builder().bucket("test").object(fileName).build()); } catch (Exception e) { e.printStackTrace(); } } /** * 获取文件外链 * * @param bucketName bucket名称 * @param objectName 文件名称 * @return url */ @SneakyThrows public static String getObjectURL(String bucketName, String objectName) { GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(bucketName) .object(objectName) // .expiry(60 * 60 * 24) .build(); return minio.getPresignedObjectUrl(build); } }
minio轮子
最新推荐文章于 2025-08-15 22:56:20 发布