springboot+vue實現(xiàn)Minio文件存儲的示例代碼
安裝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ū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Java中LocalDate的詳細(xì)方法舉例總結(jié)
這篇文章主要給大家介紹了關(guān)于Java中LocalDate詳細(xì)方法舉例的相關(guān)資料,LocalDate主要是用來處理日期的類,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09詳解通過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接口測試教程以及接口測試流程,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11springboot如何查找配置文件路徑的順序和其優(yōu)先級別
此文是在工作中遇到的關(guān)于springboot配置文件的問題,在網(wǎng)上查閱資料和自己測試之后記錄的,以便日后查閱。希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08