| | |
| | | package com.ard.file.service; |
| | | |
| | | import java.io.InputStream; |
| | | |
| | | import io.minio.GetPresignedObjectUrlArgs; |
| | | import io.minio.http.Method; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Primary; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | */ |
| | | @Primary |
| | | @Service |
| | | public class MinioSysFileServiceImpl implements ISysFileService |
| | | { |
| | | public class MinioSysFileServiceImpl implements ISysFileService { |
| | | @Autowired |
| | | private MinioConfig minioConfig; |
| | | |
| | |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public String uploadFile(MultipartFile file) throws Exception |
| | | { |
| | | public String uploadFile(MultipartFile file) throws Exception { |
| | | InputStream inputStream = null; |
| | | try |
| | | { |
| | | try { |
| | | String fileName = FileUploadUtils.extractFilename(file); |
| | | inputStream = file.getInputStream(); |
| | | PutObjectArgs args = PutObjectArgs.builder() |
| | |
| | | .build(); |
| | | client.putObject(args); |
| | | return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("Minio Failed to upload file", e); |
| | | } |
| | | finally |
| | | { |
| | | } finally { |
| | | IoUtils.closeQuietly(inputStream); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Minio文件上传接口 |
| | | * |
| | | * @param file 上传的文件 |
| | | * @param bucketName 桶名称 |
| | | * @return 访问地址 |
| | | * @author 刘苏义 |
| | | * @description 指定桶名称上传文件 |
| | | * @date 2024/8/5 11:37 |
| | | */ |
| | | @Override |
| | | public String uploadFile(MultipartFile file, String bucketName) throws Exception { |
| | | String fileName = FileUploadUtils.extractFilename(file); |
| | | InputStream inputStream = file.getInputStream(); |
| | | PutObjectArgs args = PutObjectArgs.builder() |
| | | .bucket(bucketName) |
| | | .object(fileName) |
| | | .stream(inputStream, file.getSize(), -1) |
| | | .contentType(file.getContentType()) |
| | | .build(); |
| | | client.putObject(args); |
| | | IoUtils.closeQuietly(inputStream); |
| | | return minioConfig.getUrl() + "/" + bucketName + "/" + fileName; |
| | | } |
| | | |
| | | @Override |
| | | public String uploadFile(MultipartFile file, String bucketName, String path) throws Exception { |
| | | //判断文件是否为空 |
| | | if (null == file || 0 == file.getSize()) { |
| | | return ""; |
| | | } |
| | | //文件名 |
| | | String fileName = path + "/" + FileUploadUtils.extractFilename(file); |
| | | InputStream inputStream = file.getInputStream(); |
| | | /*上传对象*/ |
| | | PutObjectArgs putObjectArgs = PutObjectArgs |
| | | .builder() |
| | | .bucket(bucketName) |
| | | .object(fileName) |
| | | .stream(inputStream, file.getSize(), -1) |
| | | .contentType(file.getContentType()) |
| | | .build(); |
| | | client.putObject(putObjectArgs); |
| | | inputStream.close(); |
| | | /*获取url*/ |
| | | GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs |
| | | .builder() |
| | | .bucket(bucketName) |
| | | .object(fileName) |
| | | .method(Method.GET) |
| | | .build(); |
| | | String presignedObjectUrl = client.getPresignedObjectUrl(getPresignedObjectUrlArgs); |
| | | String ObjectUrl = presignedObjectUrl.substring(0, presignedObjectUrl.indexOf("?")); |
| | | return ObjectUrl; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public void deleteFile(String fileUrl) throws Exception |
| | | { |
| | | try |
| | | { |
| | | public void deleteFile(String fileUrl) throws Exception { |
| | | try { |
| | | String minioFile = StringUtils.substringAfter(fileUrl, minioConfig.getBucketName()+ "/"); |
| | | client.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucketName()).object(minioFile).build()); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("Minio Failed to delete file", e); |
| | | } |
| | | } |