欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot+vue實現(xiàn)Minio文件存儲的示例代碼

 更新時間:2024年02月29日 15:10:27   作者:今年不養(yǎng)豬只除草  
本文主要介紹了springboot+vue實現(xiàn)Minio文件存儲的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

安裝minio

首先點擊進(jìn)入MINIO官網(wǎng),進(jìn)行一個minio服務(wù)器的下載

在這里插入圖片描述

下載好了之后在本地磁盤找一個文件夾,把下載的exe放入文件夾,再新建一個文件夾準(zhǔn)備存放數(shù)據(jù)和文件

在這里插入圖片描述

在當(dāng)前目錄cmd進(jìn)入控制臺,輸入代碼

minio.exe server data

成功后會有訪問路徑以及賬號密碼

在這里插入圖片描述

訪問這個路徑,輸入剛剛控制臺打印的賬號密碼就可以進(jìn)入

http://127.0.0.1:9000

在這里插入圖片描述

進(jìn)入后創(chuàng)建buckets

在這里插入圖片描述

在這里插入圖片描述

后端

在官網(wǎng)找到依賴并導(dǎo)入

在這里插入圖片描述

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.4.3</version>
</dependency>

添加yml文件配置

# Minio配置 wyj配置
minio:
  endpoint: http://127.0.0.1:9000
  accessKey: minioadmin
  secretKey: minioadmin
  bucketName: svt-ttt

MinIoClientConfig配置文件

這里bean文件只能有這一個,如果是多人團(tuán)隊項目開發(fā)記得在測試的時候協(xié)商好
@Value的注解導(dǎo)入是spring的原生注解

package com.wedu.config;

import io.minio.MinioClient;
import lombok.Data;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/*
 * Minio的配置類
 * */
@Data
@Component
public class MinIoClientConfig {
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Value("${minio.bucket-name}")
    private String bucketName;
    /**
     * 注入minio 客戶端
     *
     * @return
     */
    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

}

工具類MinIOUtil

工具類里有很多可使用的方法,這里只用到了uploadFile

package com.wedu.modules.tain.utils;

import com.amazonaws.util.IOUtils;
import com.wedu.modules.equi.entity.ObjectItem;
import io.minio.*;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MinIOUtil {
    @Autowired
    private MinioClient minioClient;

    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.access-key}")
    private String accessKey;
    @Value("${minio.secret-key}")
    private String secretKey;
    @Value("${minio.bucket-name}")
    private String bucketName;

    /**
     * 上傳文件到指定的存儲桶中
     *
     * @param bucketName 存儲桶名稱
     * @param file 上傳的文件
     * @param objectName 存儲對象的名稱
     * @param contentType 文件的內(nèi)容類型
     * @return 文件上傳的響應(yīng)對象
     * @throws Exception 如果上傳過程中發(fā)生異常
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse uploadFile(String bucketName, MultipartFile file, String objectName, String contentType) {
        InputStream inputStream = file.getInputStream();
        return minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .contentType(contentType)
                        .stream(inputStream, inputStream.available(), -1)
                        .build());
    }

    /**
     * description: 判斷bucket是否存在,不存在則創(chuàng)建
     *
     * @return: void
     */
    public void existBucket(String name) {
        try {
            boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
            if (!exists) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 創(chuàng)建存儲bucket
     * @param bucketName 存儲bucket名稱
     * @return Boolean
     */
    public Boolean makeBucket(String bucketName) {
        try {
            minioClient.makeBucket(MakeBucketArgs.builder()
                    .bucket(bucketName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 刪除存儲bucket
     * @param bucketName 存儲bucket名稱
     * @return Boolean
     */
    public Boolean removeBucket(String bucketName) {
        try {
            minioClient.removeBucket(RemoveBucketArgs.builder()
                    .bucket(bucketName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /**
     * description: 上傳文件
     *
     * @param multipartFile
     * @return: java.lang.String

     */
    public List<String> upload(MultipartFile[] multipartFile) {
        List<String> names = new ArrayList<>(multipartFile.length);
        for (MultipartFile file : multipartFile) {
            String fileName = file.getOriginalFilename();
            String[] split = fileName.split("\\.");
            if (split.length > 1) {
                fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
            } else {
                fileName = fileName + System.currentTimeMillis();
            }
            InputStream in = null;
            try {
                in = file.getInputStream();
                minioClient.putObject(PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(fileName)
                        .stream(in, in.available(), -1)
                        .contentType(file.getContentType())
                        .build()
                );
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            names.add(fileName);
        }
        return names;
    }

    /**
     * description: 下載文件
     *
     * @param fileName
     * @return: org.springframework.http.ResponseEntity<byte [ ]>
     */
    public ResponseEntity<byte[]> download(String fileName) {
        ResponseEntity<byte[]> responseEntity = null;
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
            out = new ByteArrayOutputStream();
            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.APPLICATION_OCTET_STREAM);
            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;
    }

    /**
     * 查看文件對象
     * @param bucketName 存儲bucket名稱
     * @return 存儲bucket內(nèi)文件對象信息
     */
    public List<ObjectItem> listObjects(String bucketName) {
        Iterable<Result<Item>> results = minioClient.listObjects(
                ListObjectsArgs.builder().bucket(bucketName).build());
        List<ObjectItem> objectItems = new ArrayList<>();
        try {
            for (Result<Item> result : results) {
                Item item = result.get();
                ObjectItem objectItem = new ObjectItem();
                objectItem.setObjectName(item.objectName());
                objectItem.setSize(item.size());
                objectItems.add(objectItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return objectItems;
    }

    /**
     * 批量刪除文件對象
     * @param bucketName 存儲bucket名稱
     * @param objects 對象名稱集合
     */
    public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
        List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
        Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
        return results;
    }
}

controller調(diào)用uploadFile

    @Autowired
    private MinIOUtil minIOUtil;
    @Autowired
    private MinIoClientConfig minIoClientConfig;
    //minio
    @PostMapping("/minIoUpload")
    public R minIoUpload(@RequestParam("file") MultipartFile file) throws IOException {
        //文件名
        String fileName = file.getOriginalFilename();
        String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
        //類型
        String contentType = file.getContentType();
        minIOUtil.uploadFile(minIoClientConfig.getBucketName(), file, newFileName, contentType);
        return R.ok("上傳成功");
    }

postman測試

測試成功

在這里插入圖片描述

在這里插入圖片描述

前端

主頁面和springboot+vue實現(xiàn)oss文件存儲一樣,在彈窗的下拉框中添加一個2,訪問路徑變?yōu)樽约旱膒ost的訪問路徑即可

到此這篇關(guān)于springboot+vue實現(xiàn)Minio文件存儲的示例代碼的文章就介紹到這了,更多相關(guān)springboot vueMinio文件存儲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java高并發(fā)ScheduledThreadPoolExecutor與Timer區(qū)別

    java高并發(fā)ScheduledThreadPoolExecutor與Timer區(qū)別

    這篇文章主要為大家介紹了java高并發(fā)ScheduledThreadPoolExecutor與Timer區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • JAVA獲取HTTP請求頭的方法示例

    JAVA獲取HTTP請求頭的方法示例

    這篇文章主要介紹了JAVA獲取HTTP請求頭的方法,結(jié)合具體實例形式分析了java針對http請求頭的讀取及屬性操作技巧,需要的朋友可以參考下
    2017-06-06
  • Spark SQL 編程初級實踐詳解

    Spark SQL 編程初級實踐詳解

    這篇文章主要為大家介紹了Spark SQL 編程初級實踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Java中LocalDate的詳細(xì)方法舉例總結(jié)

    Java中LocalDate的詳細(xì)方法舉例總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java中LocalDate詳細(xì)方法舉例的相關(guān)資料,LocalDate主要是用來處理日期的類,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • 如何在Spring Boot啟動時運行定制的代碼

    如何在Spring Boot啟動時運行定制的代碼

    在本文中您將學(xué)習(xí)如何掛鉤應(yīng)用程序引導(dǎo)程序生命周期并在Spring Boot啟動時執(zhí)行代碼。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • SpringJPA?做分頁條件查詢的代碼實踐

    SpringJPA?做分頁條件查詢的代碼實踐

    相信小伙伴們的項目很多都用到SpringJPA框架的吧,對于單表的增刪改查利用jpa是很方便的,但是對于條件查詢并且分頁?是不是很多小伙伴不經(jīng)常寫到,今天給大家分享SpringJPA?做分頁條件查詢的案例代碼,感興趣的朋友一起看看吧
    2024-03-03
  • 詳解通過JDBC進(jìn)行簡單的增刪改查(以MySQL為例)

    詳解通過JDBC進(jìn)行簡單的增刪改查(以MySQL為例)

    JDBC是用于執(zhí)行SQL語句的一類Java API,通過JDBC使得我們可以直接使用Java編程來對關(guān)系數(shù)據(jù)庫進(jìn)行操作。通過封裝,可以使開發(fā)人員使用純Java API完成SQL的執(zhí)行。
    2017-01-01
  • 全網(wǎng)最全最細(xì)的jmeter接口測試教程以及接口測試流程(入門教程)

    全網(wǎng)最全最細(xì)的jmeter接口測試教程以及接口測試流程(入門教程)

    本文主要介紹了全網(wǎng)最全最細(xì)的jmeter接口測試教程以及接口測試流程,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • IDEA常用插件之代碼掃描SonarLint詳解

    IDEA常用插件之代碼掃描SonarLint詳解

    SonarLint是一款用于代碼掃描的插件,可以幫助查找隱藏的bug,下載并安裝插件后,右鍵點擊項目并選擇“Analyze”、“AnalyzewithSonarLint”,掃描完成后可以在下方查看報告
    2025-01-01
  • springboot如何查找配置文件路徑的順序和其優(yōu)先級別

    springboot如何查找配置文件路徑的順序和其優(yōu)先級別

    此文是在工作中遇到的關(guān)于springboot配置文件的問題,在網(wǎng)上查閱資料和自己測試之后記錄的,以便日后查閱。希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評論