| | |
| | | import com.gkhy.exam.common.config.MinioConfig; |
| | | import io.minio.*; |
| | | import io.minio.http.Method; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.FastByteArrayOutputStream; |
| | |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.InputStream; |
| | | import java.util.Date; |
| | | |
| | | @Component |
| | | @Slf4j |
| | | public class MinioUtils { |
| | | @Autowired |
| | | private MinioClient minioClient; |
| | |
| | | * @return boolean |
| | | */ |
| | | public Boolean bucketExists(String bucketName) { |
| | | Boolean found; |
| | | Boolean found=false; |
| | | try { |
| | | found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | log.error("minio校验bucket失败:{}",e.getMessage()); |
| | | } |
| | | return found; |
| | | } |
| | | |
| | | /** |
| | | * 文件上传 上传完成后请关闭文件流 |
| | | * |
| | | * @param fileName 带后缀的文件名 检验日期yyyyMMdd加16位随机码 |
| | | * @param stream 文件流 要上传文件的流 |
| | | */ |
| | | public void fileUploader( String fileName, InputStream stream) throws Exception { |
| | | PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName).stream(stream,-1,-1).contentType("application/octet-stream").build(); |
| | | // 使用putObject上传一个文件到文件分类 |
| | | minioClient.putObject(objectArgs); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 文件上传 |
| | |
| | | * @param file 文件 |
| | | * @return Boolean |
| | | */ |
| | | public String upload(MultipartFile file) { |
| | | public String uploadFile(MultipartFile file) { |
| | | String originalFilename = file.getOriginalFilename(); |
| | | if (StringUtils.isBlank(originalFilename)){ |
| | | throw new RuntimeException(); |
| | |
| | | //文件名称相同会覆盖 |
| | | minioClient.putObject(objectArgs); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("minio上传文件失败:{}",e.getMessage()); |
| | | return null; |
| | | } |
| | | return objectName; |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除文件 |
| | | * @param fileName |
| | | * @return |
| | | */ |
| | | public boolean removeFile(String fileName){ |
| | | try { |
| | | RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(minioConfig.getBucketName()) |
| | | .object(fileName).build(); |
| | | minioClient.removeObject(removeObjectArgs); |
| | | return true; |
| | | }catch (Exception e){ |
| | | return false; |
| | | } |
| | | } |
| | | } |