|
@@ -0,0 +1,130 @@
|
|
|
|
|
+package com.zksy.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.zksy.config.MinIOConfigProperties;
|
|
|
|
|
+import com.zksy.service.MinioFileStorageService;
|
|
|
|
|
+import io.minio.*;
|
|
|
|
|
+import io.minio.errors.*;
|
|
|
|
|
+import io.minio.messages.ObjectMetadata;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.security.InvalidKeyException;
|
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class MinioFileStorageServiceImpl implements MinioFileStorageService {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private MinioClient minioClient;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private MinIOConfigProperties minIOConfigProperties;
|
|
|
|
|
+
|
|
|
|
|
+ @PostConstruct
|
|
|
|
|
+ public void init() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ String bucketName = minIOConfigProperties.getBucket();
|
|
|
|
|
+ boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
|
|
|
|
+ if (!found) {
|
|
|
|
|
+ minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
|
|
|
|
+ String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::"+bucketName+"/*\"]}]}";
|
|
|
|
|
+ minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("初始化MinIO成功");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("初始化MinIO失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteFile(String fileName) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ minioClient.removeObject(RemoveObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(fileName).build());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("删除文件失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void downLoadFile(String filePath, HttpServletResponse response) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
|
|
|
|
|
+ .bucket(minIOConfigProperties.getBucket())
|
|
|
|
|
+ .object(filePath)
|
|
|
|
|
+ .build()
|
|
|
|
|
+ );
|
|
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + filePath);
|
|
|
|
|
+ OutputStream out = response.getOutputStream();
|
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
|
+ int length;
|
|
|
|
|
+ while ((length = inputStream.read(buffer)) > 0) {
|
|
|
|
|
+ out.write(buffer, 0, length);
|
|
|
|
|
+ }
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ out.close();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("下载文件失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file 文件
|
|
|
|
|
+ * @param businessType 业务类型
|
|
|
|
|
+ * @return filePath 文件的路径
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String uploadFile(MultipartFile file, String businessType) {
|
|
|
|
|
+ //获取文件后缀
|
|
|
|
|
+ String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
|
|
+ //获取文件类型
|
|
|
|
|
+ String fileType = suffix.replace(".", "");
|
|
|
|
|
+ //获取主键id
|
|
|
|
|
+ Long uuid = UUID.randomUUID().getMostSignificantBits() & 0x7fffffffffffffffL;
|
|
|
|
|
+ String filePath = new SimpleDateFormat("yyyy/MM/dd").format(Calendar.getInstance().getTime());
|
|
|
|
|
+ filePath = "/" + filePath + "/" + businessType + "/" + fileType + "/" + uuid + suffix;
|
|
|
|
|
+ try {
|
|
|
|
|
+ minioClient.putObject(
|
|
|
|
|
+ PutObjectArgs
|
|
|
|
|
+ .builder()
|
|
|
|
|
+ .bucket(minIOConfigProperties.getBucket())
|
|
|
|
|
+ .object(filePath)
|
|
|
|
|
+ .stream(file.getInputStream(), file.getSize(), ObjectWriteArgs.MIN_MULTIPART_SIZE)
|
|
|
|
|
+ .build()
|
|
|
|
|
+ );
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("上传文件失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return filePath;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<String> uploadFileBatch(List<MultipartFile> files, String businessType) {
|
|
|
|
|
+ List<String> resList = new ArrayList<>();
|
|
|
|
|
+ files.forEach(q->{
|
|
|
|
|
+ try {
|
|
|
|
|
+ String s = this.uploadFile(q, businessType);
|
|
|
|
|
+ resList.add(s);
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ log.error("上传文件失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ return resList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteFileBatch(List<String> filePaths) {
|
|
|
|
|
+ filePaths.forEach(this::deleteFile);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|