天天看點

2022-03-10 MinIO 對象存儲(OSS)概述及基本使用前言概述使用代碼

2022-03-10 MinIO概述及基本使用

  • 前言
  • 概述
  • 使用代碼

前言

記錄一下,下面概述附連結,介紹的非常詳細;以及項目中使用的工具類代碼。

概述

高性能對象存儲MinIO介紹

MinIO 對象存儲

MinIO API文檔

使用代碼

/**
 * minio工具類
 * @Author zsl
 * @Date 2022/3/8 18:14
 * @Email [email protected]
 */
@Slf4j
@Component
public class MinioUtil {

    @Value("${minio.bucketName}")
    String bucketName;

    @Autowired
    MinioClient minioClient;

    /**
     * 擷取 minio對象
     *
     * @param filename 檔案名
     * @return 對象,若不存在則為null
     */
    public ObjectStat getObject(String filename) {
        return getObject(bucketName, filename);
    }

    /**
     * 擷取 minio對象
     *
     * @param bucketName 桶資訊
     * @param filename 檔案名
     * @return 對象,若不存在則為null
     */
    public ObjectStat getObject(String bucketName, String filename) {
        try {
            return minioClient.statObject(bucketName, filename);
        } catch (Exception e) {
//            e.printStackTrace();
            log.info("對象 {} 不存在桶 {}", filename, bucketName);
        }
        return null;
    }

    /**
     * 擷取 minio流
     *
     * @param filename
     * @return
     */
    public InputStream getInputStream(String filename) {
        return getInputStream(bucketName, filename);
    }

    /**
     * 擷取 minio流
     *
     * @param bucketName 桶名稱
     * @param filename 檔案名
     * @return 檔案流
     */
    @SneakyThrows
    public InputStream getInputStream(String bucketName, String filename) {
        return minioClient.getObject(bucketName, filename);
    }

    /**
     * 擷取全部 bucket
     *
     * @return all bucket
     */
    @SneakyThrows
    public List<Bucket> getAllBucket() {
        return minioClient.listBuckets();
    }

    /**
     * 判斷 bucket是否存在
     *
     * @param bucketName 桶名稱
     * @return true 存在
     */
    @SneakyThrows
    public boolean bucketExists(String bucketName) {
        return minioClient.bucketExists(bucketName);
    }

    /**
     * 建立 bucket
     *
     * @param bucketName 桶名稱
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
        if (!bucketExists(bucketName)) {
            minioClient.makeBucket(bucketName);
        }
    }

    /**
     * 擷取 minio下載下傳位址
     *
     * @param filename 檔案名
     */
    public String getUrl(String filename) {
        return getUrl(bucketName, filename);
    }

    /**
     * 擷取 minio下載下傳位址
     *
     * @param bucketName 桶名
     * @param filename 檔案名
     */
    @SneakyThrows
    public String getUrl(String bucketName, String filename) {
        return minioClient.presignedGetObject(bucketName, filename);
    }

    /**
     * 檔案上傳
     *
     * @param file 檔案
     * @return 檔案url
     */
    public String upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        upload(bucketName, file);
        log.info("上傳結束 {}", file.getOriginalFilename());
        return getUrl(originalFilename);
    }

    /**
     * 檔案上傳
     *
     * @param bucketName 桶名稱
     * @param file 檔案
     * @return 檔案url
     */
    public String upload(String bucketName, MultipartFile file) {
        InputStream is = null;
        try {
            is = file.getInputStream();
            String filename = file.getOriginalFilename();
            upload(bucketName, filename, is);
            return getUrl(bucketName, filename);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtil.close(is);
        }
        return null;
    }

    /**
     * 檔案上傳
     *
     * @param filename 檔案名
     * @param is    流
     * @return 檔案url
     */
    public String upload(String filename, InputStream is) {
        return upload(bucketName, filename, is);
    }

    /**
     * 檔案上傳
     *
     * @param bucketName 桶名稱
     * @param filename 檔案名
     * @param is 流
     * @return 檔案url
     */
    @SneakyThrows
    public String upload(String bucketName, String filename, InputStream is) {
        minioClient.putObject(bucketName, filename, is, new PutObjectOptions(is.available(), -1));
        return getUrl(filename);
    }

    /**
     * 檔案下載下傳
     *
     * @param filename 檔案名稱
     * @param request 請求
     * @param response 響應
     */
    public void download(String filename, HttpServletRequest request, HttpServletResponse response) {
        download(bucketName, filename, request, response);
    }

    /**
     * 檔案下載下傳
     *
     * @param bucketName 桶名稱
     * @param filename 檔案名稱
     * @param request 請求
     * @param response 響應
     */
    public void download(String bucketName, String filename, HttpServletRequest request, HttpServletResponse response) {
        // 擷取 Mine類型
        ServletContext servletContext = request.getServletContext();
        String mimeType = servletContext.getMimeType(filename);
        response.setContentType(mimeType);

        //告訴用戶端收到的資料是用于下載下傳使用(通過響應頭)
        try {
            if (request.getHeader("User-Agent").contains("Firefox")) {
                //火狐浏覽器 Base64
                response.setHeader("Content-Disposition", "attachment; filename==?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("UTF-8")) + "?=");
            } else {
                //谷歌 IE URLEncoder
                response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.debug("設定響應頭檔案名轉碼失敗!");
            return ;
        }

        // 擷取流
        InputStream is = null;
        ServletOutputStream os = null;

        byte[] bytes = new byte[8 * 1024];
        int len;
        try {
            is = minioClient.getObject(bucketName, filename);
            os = response.getOutputStream();
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtil.close(is, os);
        }
    }

    /**
     * 删除 minio檔案
     *
     * @param filename 檔案名
     */
    public void del(String filename) {
        del(bucketName, filename);
    }

    /**
     * 删除 minio檔案
     *
     * @param bucketName 桶名稱
     * @param filename 檔案名
     */
    @SneakyThrows
    public void del(String bucketName, String filename) {
        minioClient.removeObject(bucketName, filename);
    }

    /**
     * 設定桶政策
     *
     * @param bucketName 桶名稱
     * @param policy 政策
     */
    @SneakyThrows
    public void setBucketPolicy(String bucketName, String policy) {
        minioClient.setBucketPolicy(bucketName, policy);
    }
}