| | |
| | | package com.example.jz.service.impl; |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.example.jz.config.MinIOConfig; |
| | | import com.example.jz.exception.BusinessException; |
| | | import com.example.jz.service.MinIOService; |
| | | import io.minio.GetPresignedObjectUrlArgs; |
| | | import io.minio.MinioClient; |
| | | import io.minio.errors.*; |
| | | import io.minio.http.Method; |
| | | import io.minio.PutObjectOptions; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | |
| | | @Service |
| | | public class MinIOServiceImpl implements MinIOService { |
| | |
| | | MinioClient minioClient; |
| | | @Autowired |
| | | MinIOConfig minIOConfig; |
| | | |
| | | @Value("${minio.url}") |
| | | String url; |
| | | |
| | | @Value("${minio.bucketName}") |
| | | String bucketName; |
| | | |
| | | private static final String PATH = "http://221.237.182.28:19000/"; |
| | | |
| | | |
| | | @Override |
| | | public String getPreviewFileUrl(String fileName){ |
| | | String res = null; |
| | | public String getPreviewFileUrl(String fileName) { |
| | | try { |
| | | res = minioClient.presignedGetObject(minIOConfig.getBucketName(), fileName); |
| | | } catch (ErrorResponseException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (InsufficientDataException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (InternalException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (InvalidBucketNameException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (InvalidExpiresRangeException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (InvalidKeyException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (InvalidResponseException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (ServerException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (XmlParserException e) { |
| | | throw new RuntimeException(e); |
| | | return PATH + bucketName + '/' + fileName; |
| | | } catch (Exception e) { |
| | | throw new BusinessException("获取文件预览地址失败"); |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | @Override |
| | | public String upload(MultipartFile file) { |
| | | String objectName = null; |
| | | try { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); |
| | | objectName = sdf.format(new Date()) + "/" + IdUtil.simpleUUID() + "." + file.getContentType().split("/")[1]; |
| | | PutObjectOptions putObjectOptions = new PutObjectOptions(file.getInputStream().available(), -1); |
| | | putObjectOptions.setContentType(file.getContentType()); |
| | | minioClient.putObject(minIOConfig.getBucketName(), objectName, file.getInputStream(), putObjectOptions); |
| | | } catch (Exception e) { |
| | | throw new BusinessException("上传文件失败"); |
| | | } |
| | | return objectName; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean delete(String fileName) { |
| | | try { |
| | | minioClient.removeObject(minIOConfig.getBucketName(), fileName); |
| | | } catch (Exception e) { |
| | | throw new BusinessException("删除文件失败"); |
| | | } |
| | | return true; |
| | | } |
| | | } |