MinIo對象存儲文件上傳,下載,預覽,批量上傳,創建桶等
MinIo 操作工具類
MinIo 官網地址 https://min.io/
package com.ming.utils; import io.minio.*; import io.minio.Result; import io.minio.errors.*; import io.minio.http.Method; import io.minio.messages.Bucket; import io.minio.messages.DeleteError; import io.minio.messages.DeleteObject; import io.minio.messages.Item; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.time.ZonedDateTime; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * Description minio 單例模式 工具類型、 * Create By Mr.Fang * * @time 2022/7/10 9:57 **/ public class MinioUtils { private static MinioUtils minioUtils = new MinioUtils(); private MinioClient minioClient; private static final String endpoint = "http://xxx.xxx.xxx.xx:9000"; private static final String accessKey = "xxxxxx"; // 你的 key private static final String secretKey = "xxxxxxxxxxxx"; //你的 secret private String bucketName = "xxxx"; //桶名稱 private MinioUtils() { this.init(); } /** * 初始化創建對象 */ private void init() { minioClient = MinioClient.builder() .endpoint(endpoint) .credentials(accessKey, secretKey).build(); } /** * Description 獲取 MinioUtils 對象 * Create By Mr.Fang * * @return com.ming.utils.MinioUtils * @time 2022/7/10 10:18 **/ public static MinioUtils getInstance() { return minioUtils; } /** * Description 獲取 MinioClient 對象 * Create By Mr.Fang * * @return com.ming.utils.MinioUtils * @time 2022/7/10 10:18 **/ public MinioClient getMinioClient() { return minioClient; } /** * 設置 桶名稱 * * @param bucketName */ public void setBucketName(String bucketName) { this.bucketName = bucketName; } /** * Description 判斷桶是否存在 * Create By Mr.Fang * * @param bucket * @return java.lang.Boolean * @time 2022/7/10 9:57 **/ public Boolean existBucket(String bucket) { try { return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); } catch (Exception e) { e.printStackTrace(); return false; } } /** * Description 創建一個桶 * Create By Mr.Fang * * @param bucket 桶名稱 * @return java.lang.Boolean * @time 2022/7/10 9:56 **/ public Boolean createBucket(String bucket) { try { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * Description 上傳本地文件 * Create By Mr.Fang * * @param path 本地文件路徑 * @param object 文件名 * @return java.lang.Boolean * @time 2022/7/10 9:55 **/ public Boolean uploadObject(String path, String object) { try { minioClient.uploadObject( UploadObjectArgs.builder() .bucket(bucketName) .object(object) .filename(path) .build()); } catch (Exception e) { e.printStackTrace(); } return true; } /** * Description 以流的方式上傳文件 * Create By Mr.Fang * * @param in input 流 * @param object 文件名 * @param size 文件大小 * @return java.lang.Boolean * @time 2022/7/10 9:55 **/ public Boolean uploadObject(InputStream in, String object, Long size) { try { minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(object) .stream(in, size, -1) .build() ); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * Description 多文件上傳 * Create By Mr.Fang * * @param objects SnowballObject對象 * @return java.lang.Boolean * @Param * @time 2022/7/10 10:24 **/ public Boolean uploadsObject(List<SnowballObject> objects) { try { minioClient.uploadSnowballObjects( UploadSnowballObjectsArgs.builder().bucket(bucketName).objects(objects).build()); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * Description 批量刪除文件對象 * Create By Mr.Fang * * @param objects 文件名稱或完整文件路徑 * @return java.util.List<io.minio.messages.DeleteError> * @time 2022/7/10 9:54 **/ public List<DeleteError> deleteObject(List<String> objects) { List<DeleteError> deleteErrors = new ArrayList<>(); List<DeleteObject> deleteObjects = objects.stream().map(value -> new DeleteObject(value)).collect(Collectors.toList()); Iterable<Result<DeleteError>> results = minioClient.removeObjects( RemoveObjectsArgs .builder() .bucket(bucketName) .objects(deleteObjects) .build()); try { for (Result<DeleteError> result : results) { DeleteError error = result.get(); deleteErrors.add(error); } } catch (Exception e) { e.printStackTrace(); } return deleteErrors; } /** * Description 下載文件到本地 * Create By Mr.Fang * * @param object 文件名 * @param output 輸出路徑 * @return void * @time 2022/7/10 9:53 **/ public void download(String object, String output) { try { minioClient.downloadObject( DownloadObjectArgs.builder() .bucket(bucketName) .object(object) .filename(output) .build()); } catch (Exception e) { e.printStackTrace(); } } /** * Description 下載 minio文件,返回流的方式,同時支持在線預覽 * Create By Mr.Fang * * @param object 文件名稱 * @param isOnline 是否在線預覽,不同文件類型請修改 setContentType * @param response 響應對象 * @return void * @time 2022/7/10 9:51 **/ public void download(String object, Boolean isOnline, HttpServletResponse response) { try (InputStream stream = minioClient.getObject( GetObjectArgs.builder() .bucket(bucketName) .object(object) .build())) { try { BufferedInputStream br = new BufferedInputStream(stream); byte[] buf = new byte[1024]; int len = 0; response.reset(); // 非常重要 if (Objects.nonNull(isOnline) && isOnline) { // 在線打開方式 response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename=" + object); } else { // 純下載方式 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + object); } OutputStream out = response.getOutputStream(); while ((len = br.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); br.close(); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } /** * Description 獲取所有桶 * Create By Mr.Fang * * @return java.util.List<io.minio.messages.Bucket> * @time 2022/7/10 9:50 **/ public List<Bucket> listBuckets() { try { return minioClient.listBuckets(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * Description 文件列表 * Create By Mr.Fang * * @param limit 范圍 1-1000 * @return java.util.List<io.minio.messages.Item> * @time 2022/7/10 9:50 **/ public List<Item> listObjects(int limit) { List<Item> objects = new ArrayList<>(); Iterable<Result<Item>> results = minioClient.listObjects( ListObjectsArgs.builder() .bucket(bucketName) .maxKeys(limit) .includeVersions(true) .build()); try { for (Result<Item> result : results) { objects.add(result.get()); } } catch (Exception e) { e.printStackTrace(); } return objects; } /** * Description 生成預覽鏈接,最大7天有效期; * 如果想永久有效,在 minio 控制臺設置倉庫訪問規則總幾率 * Create by Mr.Fang * * @param object 文件名稱 * @param contentType 預覽類型 image/gif", "image/jpeg", "image/jpg", "image/png", "application/pdf * @return java.lang.String * @Time 9:43 2022/7/10 * @Params **/ public String getPreviewUrl(String object, String contentType) { Map<String, String> reqParams = new HashMap<>(); reqParams.put("response-content-type", contentType != null ? contentType : "application/pdf"); String url = null; try { url = minioClient.getPresignedObjectUrl( GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(bucketName) .object(object) .expiry(7, TimeUnit.DAYS) .extraQueryParams(reqParams) .build()); } catch (Exception e) { e.printStackTrace(); } return url; } /** * Description 網絡文件轉儲 minio * Author Mr.Fang * * @param httpUrl 文件地址 * @return void * @Time 9:42 2022/7/10 * @Params **/ public void netToMinio(String httpUrl) { int i = httpUrl.lastIndexOf("."); String substring = httpUrl.substring(i); URL url; try { url = new URL(httpUrl); URLConnection urlConnection = url.openConnection(); // agent 模擬瀏覽器 urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"); DataInputStream dataInputStream = new DataInputStream(url.openStream()); // 臨時文件轉儲 File tempFile = File.createTempFile(UUID.randomUUID().toString().replace("-", ""), substring); FileOutputStream fileOutputStream = new FileOutputStream(tempFile); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = dataInputStream.read(buffer)) > 0) { output.write(buffer, 0, length); } fileOutputStream.write(output.toByteArray()); // 上傳minio uploadObject(tempFile.getAbsolutePath(), tempFile.getName()); dataInputStream.close(); fileOutputStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Description 文件轉字節數組 * Create By Mr.Fang * * @param path 文件路徑 * @return byte[] 字節數組 * @time 2022/7/10 10:55 **/ public byte[] fileToBytes(String path) { FileInputStream fis = null; ByteArrayOutputStream bos = null; try { bos = new ByteArrayOutputStream(); fis = new FileInputStream(path); int temp; byte[] bt = new byte[1024 * 10]; while ((temp = fis.read(bt)) != -1) { bos.write(bt, 0, temp); } bos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (Objects.nonNull(fis)) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return bos.toByteArray(); } public static void main(String[] args) throws Exception { MinioUtils minioUtils = MinioUtils.getInstance(); // 多文件上傳示例 List<SnowballObject> objects = new ArrayList<>(); byte[] bytes1 = minioUtils.fileToBytes("C:\\Users\\Administrator\\Desktop\\素材\\1.png"); byte[] bytes2 = minioUtils.fileToBytes("C:\\Users\\Administrator\\Desktop\\素材\\2.png"); byte[] bytes3 = minioUtils.fileToBytes("C:\\Users\\Administrator\\Desktop\\素材\\3.png"); objects.add(new SnowballObject("1.png", new ByteArrayInputStream(bytes1), bytes1.length, ZonedDateTime.now())); objects.add(new SnowballObject("2.png", new ByteArrayInputStream(bytes2), bytes2.length, ZonedDateTime.now())); objects.add(new SnowballObject("3.png", new ByteArrayInputStream(bytes3), bytes3.length, ZonedDateTime.now())); minioUtils.uploadsObject(objects); } }
pom 文件引入依賴
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>8.4.2</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.8.1</version> </dependency>
注意事項:MinIo sdk是最新,和舊版一些方法有差異,請求是異步的
開啟永久訪問策略
- 控制臺界面找到你要永久開放的桶
- 點擊“manage”
- 在點擊訪問規則
- 設置訪問規則“prefix” * 權限只讀就可以了
- 資源訪問路徑 http://xxx.xxx.xxx:9000/桶/文件名



哇!又賺了一天人民幣

浙公網安備 33010602011771號