Explorar el Código

minio代码上传

gao.qiang hace 2 años
padre
commit
e6f61dde0a

+ 9 - 0
base-common/pom.xml

@@ -155,6 +155,15 @@
             <artifactId>swagger-models</artifactId>
             <version>1.6.2</version>
         </dependency>
+
+        <!--分布式存储-->
+        <dependency>
+            <groupId>io.minio</groupId>
+            <artifactId>minio</artifactId>
+            <version>8.0.3</version>
+        </dependency>
+
+
     </dependencies>
 
 </project>

+ 66 - 0
base-common/src/main/java/com/ozs/common/utils/MinioUtils.java

@@ -0,0 +1,66 @@
+package com.ozs.common.utils;
+
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import io.minio.MinioClient;
+import io.minio.PutObjectArgs;
+import io.swagger.annotations.ApiImplicitParam;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.DateUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.PostConstruct;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+@Slf4j
+@Component
+public class MinioUtils {
+    @Value("${minio.endpoint}")
+    private String endpoint;
+    @Value("${minio.accessKey}")
+    private String accessKey;
+    @Value("${minio.secretKey}")
+    private String secretKey;
+    @Value("${minio.bucketName}")
+    private String bucketName;
+    
+
+
+    /**
+     * 上传
+     * @param imgInputStream
+     * @param objectName
+     * @throws Exception
+     */
+    public void minIoClientUpload(InputStream imgInputStream, String objectName) throws Exception {
+        //创建头部信息
+        Map<String, String> headers = new HashMap<>(10);
+        //添加自定义内容类型
+        headers.put("Content-Type", "application/octet-stream");
+        //添加存储类
+        headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
+        //添加自定义/用户元数据
+        Map<String, String> userMetadata = new HashMap<>(10);
+        userMetadata.put("My-Project", "Project One");
+        MinioClient minioClient =
+                MinioClient.builder()
+                        .endpoint(endpoint)
+                        .credentials(accessKey, secretKey)
+                        .build();
+
+        minioClient.putObject(
+                PutObjectArgs.builder()
+                        .bucket(bucketName)
+                        .object(objectName)
+                        .stream(imgInputStream, imgInputStream.available(), -1)
+                        .userMetadata(userMetadata)
+                        .build());
+        imgInputStream.close();
+    }
+}