最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot?Minio功能實(shí)現(xiàn)代碼

 更新時(shí)間:2023年07月05日 10:41:14   作者:_Elaina  
這篇文章主要介紹了springboot?Minio功能實(shí)現(xiàn),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.導(dǎo)入Minio相關(guān)依賴

<dependency>
  <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
       <version>8.4.4</version>
          <exclusions>
            <exclusion>
              <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
             </exclusion>
          </exclusions>
</dependency>
<dependency>
   <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
        <version>4.10.0</version>
</dependency>

2.application.yml 配置信息

spring:
# 文件上傳
  servlet:
    multipart:
      # 單個(gè)文件大小
      max-file-size: 500MB
      # 設(shè)置總上傳的文件大小
      max-request-size: 1000MB
# MinIO配置
minio:
  # 服務(wù)地址
  endpoint: http://localhost:9000
  # 文件地址
  fileHost: http://localhost:9000
  # 存儲(chǔ)桶名稱
  bucket: files
  # 用戶名
  access-key: minioadmin
  # 密碼
  secret-key: minioadmin

3.MinIO配置類

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * MinIO配置類
 *
 * @date 2023/6/28
 */
@Configuration
public class MinioConfig {
    private MinioProperties minioProperties;
    @Autowired
    public void setMinioProperties(MinioProperties minioProperties) {
        this.minioProperties = minioProperties;
    }
    /**
     * 初始化客戶端
     * @return 客戶端
     */
    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
    }
}

4.Minio實(shí)體類

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
 * @date 2023/6/28
 */
@Configuration
@ConfigurationProperties("minio")
public class Minio {
    /**
     * 服務(wù)地址
     */
    private String endpoint;
    /**
     * 文件預(yù)覽地址
     */
    private String preview;
    /**
     * 存儲(chǔ)桶名稱
     */
    private String bucket;
    /**
     * 用戶名
     */
    private String accessKey;
    /**
     * 密碼
     */
    private String secretKey;
    public String getEndpoint() {
        return endpoint;
    }
    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }
    public String getPreview() {
        return preview;
    }
    public void setPreview(String preview) {
        this.preview = preview;
    }
    public String getBucket() {
        return bucket;
    }
    public void setBucket(String bucket) {
        this.bucket = bucket;
    }
    public String getAccessKey() {
        return accessKey;
    }
    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }
    public String getSecretKey() {
        return secretKey;
    }
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
}

5.Minio工具類

import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
 * MinIO實(shí)現(xiàn)類
 *
 * @date 2023/6/28
 */
@Slf4j
@Component
public class MinioUtil {
    private static MinioClient minioClient;
    /**
     * setter注入
     *
     * @param minioClient 客戶端
     */
    @Autowired
    public void setMinioClient(MinioClient minioClient) {
        MinioUtil.minioClient = minioClient;
    }
    /**
     * 啟動(dòng)SpringBoot容器的時(shí)候初始化Bucket,如果沒(méi)有Bucket則創(chuàng)建
     */
    public static void createBucket(String bucketName) throws Exception {
        if (!bucketExists(bucketName)) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }
    /**
     * 判斷Bucket是否存在
     *
     * @return true:存在,false:不存在
     */
    public static boolean bucketExists(String bucketName) throws Exception {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }
    /**
     * 獲得Bucket策略
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @return Bucket策略
     */
    public static String getBucketPolicy(String bucketName) throws Exception {
        return minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
    }
    /**
     * 獲得所有Bucket列表
     *
     * @return Bucket列表
     */
    public static List<Bucket> getAllBuckets(MinioClient minioClient) throws Exception {
        return minioClient.listBuckets();
    }
    /**
     * 根據(jù)存儲(chǔ)桶名稱獲取其相關(guān)信息
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @return 相關(guān)信息
     */
    public static Optional<Bucket> getBucket(String bucketName) throws Exception {
        return getAllBuckets(minioClient)
                .stream()
                .filter(b -> b.name().equals(bucketName))
                .findFirst();
    }
    /**
     * 根據(jù)存儲(chǔ)桶名稱刪除Bucket,true:刪除成功;false:刪除失敗,文件或已不存在
     *
     * @param bucketName 存儲(chǔ)桶名稱
     */
    public static void removeBucket(String bucketName) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }
    /**
     * 判斷文件是否存在
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件名
     * @return true:存在;false:不存在
     */
    public static boolean isObjectExist(String bucketName, String objectName) {
        boolean exist = true;
        try {
            minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
        } catch (Exception e) {
            exist = false;
        }
        return exist;
    }
    /**
     * 判斷文件夾是否存在
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件夾名稱
     * @return true:存在;false:不存在
     */
    public static boolean isFolderExist(String bucketName, String objectName) {
        boolean exist = false;
        try {
            ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder()
                    .bucket(bucketName)
                    .prefix(objectName)
                    .recursive(false)
                    .build();
            Iterable<Result<Item>> results = minioClient.listObjects(listObjectsArgs);
            for (Result<Item> result : results) {
                Item item = result.get();
                if (item.isDir() && objectName.equals(item.objectName())) {
                    exist = true;
                }
            }
        } catch (Exception e) {
            exist = false;
        }
        return exist;
    }
    /**
     * 根據(jù)文件前綴查詢文件
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param prefix     前綴
     * @param recursive  是否使用遞歸查詢
     * @return MinioItem列表
     */
    public static List<Item> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) throws Exception {
        List<Item> list = new ArrayList<>();
        ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder()
                .bucket(bucketName)
                .prefix(prefix)
                .recursive(recursive)
                .build();
        Iterable<Result<Item>> objectsIterator = minioClient.listObjects(listObjectsArgs);
        if (objectsIterator != null) {
            for (Result<Item> o : objectsIterator) {
                Item item = o.get();
                list.add(item);
            }
        }
        return list;
    }
    /**
     * 獲取文件流
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件名
     * @return 二進(jìn)制流
     */
    public static InputStream getObject(String bucketName, String objectName) throws Exception {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        return minioClient.getObject(getObjectArgs);
    }
    /**
     * 斷點(diǎn)下載
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件名稱
     * @param offset     起始字節(jié)的位置
     * @param length     要讀取的長(zhǎng)度
     * @return 二進(jìn)制流
     */
    public InputStream getObject(String bucketName, String objectName, long offset, long length) throws Exception {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .offset(offset)
                .length(length)
                .build();
        return minioClient.getObject(getObjectArgs);
    }
    /**
     * 獲取路徑下文件列表
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param prefix     文件名稱
     * @param recursive  是否遞歸查找,false:模擬文件夾結(jié)構(gòu)查找
     * @return 二進(jìn)制流
     */
    public static Iterable<Result<Item>> listObjects(String bucketName, String prefix, boolean recursive) {
        ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder()
                .bucket(bucketName)
                .prefix(prefix)
                .recursive(recursive)
                .build();
        return minioClient.listObjects(listObjectsArgs);
    }
    /**
     * 使用MultipartFile進(jìn)行文件上傳
     *
     * @param bucketName  存儲(chǔ)桶名稱
     * @param file        文件名
     * @param objectName  對(duì)象名
     * @return ObjectWriteResponse對(duì)象
     */
    public static ObjectWriteResponse uploadFile(String bucketName, MultipartFile file, String objectName) throws Exception {
        InputStream inputStream = file.getInputStream();
        Optional<MediaType> optional = MediaTypeFactory.getMediaType(objectName);
        String mediaType = optional.orElseThrow(() -> new RuntimeException("文件類型暫不支持")).toString();
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .contentType(mediaType)
                .stream(inputStream, inputStream.available(), -1)
                .build();
        return minioClient.putObject(putObjectArgs);
    }
    /**
     * 上傳本地文件
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 對(duì)象名稱
     * @param fileName   本地文件路徑
     */
    public static ObjectWriteResponse uploadFile(String bucketName, String objectName, String fileName) throws Exception {
        UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .filename(fileName)
                .build();
        return minioClient.uploadObject(uploadObjectArgs);
    }
    /**
     * 通過(guò)流上傳文件
     *
     * @param bucketName  存儲(chǔ)桶名稱
     * @param objectName  文件對(duì)象
     * @param inputStream 文件流
     */
    public static ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) throws Exception {
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(inputStream, inputStream.available(), -1)
                .build();
        return minioClient.putObject(putObjectArgs);
    }
    /**
     * 創(chuàng)建文件夾或目錄
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 目錄路徑
     */
    public static ObjectWriteResponse createDir(String bucketName, String objectName) throws Exception {
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(new ByteArrayInputStream(new byte[]{}), 0, -1)
                .build();
        return minioClient.putObject(putObjectArgs);
    }
    /**
     * 獲取文件信息, 如果拋出異常則說(shuō)明文件不存在
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件名稱
     */
    public static String getFileStatusInfo(String bucketName, String objectName) throws Exception {
        StatObjectArgs statObjectArgs = StatObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        return minioClient.statObject(statObjectArgs).toString();
    }
    /**
     * 拷貝文件
     *
     * @param bucketName    存儲(chǔ)桶名稱
     * @param objectName    文件名
     * @param srcBucketName 目標(biāo)存儲(chǔ)桶
     * @param srcObjectName 目標(biāo)文件名
     */
    public static ObjectWriteResponse copyFile(String bucketName, String objectName, String srcBucketName, String srcObjectName) throws Exception {
        return minioClient.copyObject(CopyObjectArgs.builder()
                .source(CopySource.builder()
                        .bucket(bucketName)
                        .object(objectName).build())
                .bucket(srcBucketName)
                .object(srcObjectName).build());
    }
    /**
     * 刪除文件
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件名稱
     */
    public static void removeFile(String bucketName, String objectName) throws Exception {
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        minioClient.removeObject(removeObjectArgs);
    }
    /**
     * 批量刪除文件
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param keys       需要?jiǎng)h除的文件列表
     */
    public static void removeFiles(String bucketName, List<String> keys) {
        keys.forEach(key -> {
            try {
                removeFile(bucketName, key);
            } catch (Exception e) {
                log.error("批量刪除失??!error:{0}", e);
            }
        });
    }
    /**
     * 獲取文件外鏈
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件名
     * @param expires    過(guò)期時(shí)間 <=7 秒 (外鏈有效時(shí)間(單位:秒))
     * @return 文件外鏈
     */
    public static String getPreSignedObjectUrl(String bucketName, String objectName, Integer expires) throws Exception {
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                .expiry(expires)
                .bucket(bucketName)
                .object(objectName)
                .build();
        return minioClient.getPresignedObjectUrl(args);
    }
    /**
     * 獲得文件外鏈
     *
     * @param bucketName 存儲(chǔ)桶名稱
     * @param objectName 文件名
     * @return 文件外鏈
     */
    public static String getPreSignedObjectUrl(String bucketName, String objectName) throws Exception {
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .method(Method.GET)
                .build();
        return minioClient.getPresignedObjectUrl(args);
    }
    /**
     * 將URLDecoder編碼轉(zhuǎn)成UTF8
     *
     * @param str 字符串
     * @return 編碼
     */
    public static String getUtf8ByDecoder(String str) throws UnsupportedEncodingException {
        String url = str.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
        return URLDecoder.decode(url, "UTF-8");
    }
}

6.Minio控制類

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
@RestController
@RequestMapping("/minio")
@CrossOrigin
public class MinioController {
    @Autowired
    private Minio minio;
    @Autowired
    private MinioUtil minioService;
    /**
     * 上傳文件
     */
    @PostMapping(value = "/upload")
    public String upload(@RequestParam(name = "file") MultipartFile multipartFile) throws Exception {
        String fileName = multipartFile.getOriginalFilename();
        minioService.createBucket(minio.getBucket());
        minioService.uploadFile(minio.getBucket(), multipartFile, fileName);
        return minioService.getPreSignedObjectUrl(minio.getBucket(), fileName);
    }
    /**
     * 下載文件
     */
    @GetMapping(value = "/download")
    public ResponseEntity<byte[]> download(@RequestParam(name = "fileName") String fileName) {
        ResponseEntity<byte[]> responseEntity = null;
        ByteArrayOutputStream out = null;
        InputStream in = null;
        try{
            out = new ByteArrayOutputStream();
            in = minioService.getObject(minio.getBucket(),fileName);
            IOUtils.copy(in, out);
            //封裝返回值
            byte[] bytes = out.toByteArray();
            HttpHeaders headers = new HttpHeaders();
            try {
                headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            headers.setContentLength(bytes.length);
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            headers.setAccessControlExposeHeaders(Arrays.asList("*"));
            responseEntity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseEntity;
    }
}

到此這篇關(guān)于springboot Minio功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot Minio功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合郵件發(fā)送的四種方法

    SpringBoot整合郵件發(fā)送的四種方法

    這篇文章主要介紹了SpringBoot整合郵件發(fā)送的四種方法,SpringBoot中集成了發(fā)送郵件的功能,本文做了進(jìn)一步優(yōu)化,需要的朋友可以參考下
    2023-03-03
  • SpringBoot中全局異常處理的5種實(shí)現(xiàn)方式小結(jié)

    SpringBoot中全局異常處理的5種實(shí)現(xiàn)方式小結(jié)

    在實(shí)際開(kāi)發(fā)中,異常處理是一個(gè)非常重要的環(huán)節(jié),合理的異常處理機(jī)制不僅能提高系統(tǒng)的健壯性,還能大大提升用戶體驗(yàn),下面我們就來(lái)看看SpringBoot中全局異常處理的5種實(shí)現(xiàn)方式吧
    2025-03-03
  • java自定注解完整示例代碼

    java自定注解完整示例代碼

    Java注解是一種元數(shù)據(jù),可以提供有關(guān)程序代碼的額外信息,它們可以用于標(biāo)記類、方法、字段等元素,它允許我們?cè)谠创a中嵌入元數(shù)據(jù)這篇文章主要介紹了java自定注解的相關(guān)資料,需要的朋友可以參考下
    2026-01-01
  • 舉例講解Java中do-while語(yǔ)句的使用方法

    舉例講解Java中do-while語(yǔ)句的使用方法

    這篇文章主要介紹了Java中do-while語(yǔ)句的使用方法例子,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-10-10
  • springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問(wèn)題

    springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問(wèn)題

    這篇文章主要介紹了springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 詳解Spring中的Environment外部化配置管理

    詳解Spring中的Environment外部化配置管理

    本文主要介紹了Spring中的Environment外部化配置管理,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 解決maven導(dǎo)入依賴失敗,說(shuō)找不到依賴的問(wèn)題

    解決maven導(dǎo)入依賴失敗,說(shuō)找不到依賴的問(wèn)題

    本文介紹了在多模塊開(kāi)發(fā)項(xiàng)目中遇到Maven依賴導(dǎo)入失敗的問(wèn)題,以及解決該問(wèn)題的步驟,作者通過(guò)手動(dòng)下載jar包并使用Maven命令將其安裝到本地倉(cāng)庫(kù),解決了項(xiàng)目中出現(xiàn)的依賴問(wèn)題
    2025-11-11
  • Java?20在Windows11系統(tǒng)下的簡(jiǎn)易安裝教程

    Java?20在Windows11系統(tǒng)下的簡(jiǎn)易安裝教程

    這篇文章主要給大家介紹了關(guān)于Java?20在Windows11系統(tǒng)下的簡(jiǎn)易安裝教程,學(xué)習(xí)Java的同學(xué),第一步就是安裝好Java環(huán)境,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Java @Autowired注解底層原理詳細(xì)分析

    Java @Autowired注解底層原理詳細(xì)分析

    @Autowired注解可以用在類屬性,構(gòu)造函數(shù),setter方法和函數(shù)參數(shù)上,該注解可以準(zhǔn)確地控制bean在何處如何自動(dòng)裝配的過(guò)程。在默認(rèn)情況下,該注解是類型驅(qū)動(dòng)的注入
    2022-11-11
  • Java 中的偽共享詳解及解決方案

    Java 中的偽共享詳解及解決方案

    這篇文章主要介紹了Java 中的偽共享詳解及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02

最新評(píng)論

怀宁县| 友谊县| 全南县| 巴彦县| 祁阳县| 怀宁县| 西平县| 皮山县| 怀远县| 汝南县| 乌拉特前旗| 白朗县| 汽车| 东港市| 封开县| 青海省| 巴彦县| 易门县| 汉川市| 吉首市| 淮南市| 德保县| 莆田市| 杂多县| 金阳县| 博兴县| 昌黎县| 巴东县| 新宾| 九龙县| 鄂尔多斯市| 兴海县| 班玛县| 民乐县| 二连浩特市| 当涂县| 长武县| 包头市| 通河县| 若羌县| 米泉市|