最近在寫項目的時候,使用了富文本編輯器wangEditor,其中有一個功能是圖片上傳,由於以前已經有一個搭建好的MinIO服務且提供了Java SDK,在實現這個功能的時候也踩了一下坑,將該功能記錄以下。javascript
在Thymeleaf中整合wangEditor須要js文件,我使用的是CDN引入css
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>wangEditor demo</title> <link href="https://cdn.staticfile.org/wangEditor/3.1.1/wangEditor.min.css" rel="stylesheet"/> </head> <body> <div id="editor"> <p>歡迎使用 <b>wangEditor</b> 富文本編輯器</p> </div> <!-- 注意, 只須要引用 JS,無需引用任何 CSS !!!--> <script src="https://cdn.staticfile.org/wangEditor/3.1.1/wangEditor.min.js"></script> <script type="text/javascript"> var E = window.wangEditor var editor = new E('#editor') // 字體 editor.customConfig.fontNames = [ '宋體', '微軟雅黑', 'Arial', 'Tahoma', 'Verdana', "JetBrains Mono" ], // 後端對應的字段 editor.customConfig.uploadFileName = 'file'; // 後端圖片上傳接口 editor.customConfig.uploadImgServer = '/resource/uploadResource', // 文件最大尺寸 editor.customConfig.uploadImgMaxSize = 10 * 1024 * 1024, // 文件上傳數量 editor.customConfig.uploadImgMaxLength = 5, editor.customConfig.customAlert = function (info) { // info 是須要提示的內容 alert('自定義提示:' + info) } editor.create() </script> </body> </html>
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>3.0.10</version> </dependency>
import io.minio.MinioClient; import io.minio.errors.*; import io.minio.policy.PolicyType; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.io.InputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; @Slf4j @Component public class MinIoService { @Value(value = "${minio.url}") private String url; @Value(value = "${minio.accessKey}") private String accessKey; @Value(value = "${minio.secretKey}") private String secretKey; /** * 獲取MinioClient * @return * @throws InvalidPortException * @throws InvalidEndpointException */ private MinioClient minioClient() throws InvalidPortException, InvalidEndpointException { return new MinioClient(url, accessKey, secretKey, true); } /** * minio文件上傳 * * @param bucketName 存儲桶 * @param fileName 文件名 * @param inputStream 輸入流 * @param contentType 文件類型 * @param size 文件大小 * @return * @throws InvalidPortException * @throws InvalidEndpointException * @throws IOException * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InsufficientDataException * @throws InvalidArgumentException * @throws InternalException * @throws NoResponseException * @throws InvalidBucketNameException * @throws XmlPullParserException * @throws ErrorResponseException * @throws RegionConflictException * @throws InvalidObjectPrefixException */ public String uploadFile(String bucketName, String fileName, InputStream inputStream, String contentType, long size) throws InvalidPortException, InvalidEndpointException, IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, RegionConflictException, InvalidObjectPrefixException { MinioClient client = minioClient(); if (!client.bucketExists(bucketName)) { client.makeBucket(bucketName); // 設置存儲桶策略 client.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY); } client.putObject(bucketName, fileName, inputStream, size, contentType); return client.getObjectUrl(bucketName, fileName); } }
有多種方式能夠實例化MinioClient
,具體請參考API,因爲以前搭建MinIO是經過Nginx代理並配置了HTTPS,因此secure
參數設置爲空true
。html
@Slf4j @Controller @RequestMapping(value = "resource") public class ResourceController { @Autowired private ResourceService resourceService; @ResponseBody @PostMapping(value = "uploadResource") public Map<String, Object> uploadResource(@RequestParam(value = "file") MultipartFile file) { return resourceService.saveResource(file); } }
緣由:沒有配置存儲桶策略或存儲桶策略配置錯誤
目前瞭解到有3種方式修改存儲桶策略:java
mc
client.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY);
原文地址:https://www.haicheng.website/...web