Minio整合SpringBoot
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
minio:
bucket: "桶名"
host: "192.168.1.123:5000"
url: "${minio.host}/${minio.bucket}/"
access-key: ⽤户名
secret-key: 密码
import io.minio.*;
import s.*;
import ssages.Item;
import org.apachemons.io.IOUtils;
import org.apachemons.lang.StringUtils;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Bean;
import k.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import static io.minio.ErrorCode.NO_SUCH_KEY;
import static io.minio.http.Method.GET;
@Component
public class MinioHelper {
@Value(value = "${minio.bucket}")
private String bucket;
@Value(value = "${minio.host}")
private String host;
@Value(value = "${minio.url}")
private String url;
@Value(value = "${minio.access-key}")
private String accessKey;
@Value(value = "${minio.secret-key}")
private String secretKey;
@Autowired
private MinioClient minioClient;
@Bean
@Bean
public MinioClient getMinioClient() {
MinioClient minioClient=MinioClient.builder()
.endpoint(host).credentials(accessKey,secretKey).build();
log.info("minioClient init success.");
return minioClient;
}
private final Logger log = Logger(MinioHelper.class);
/**
* 根据名字获得返回类型
* @param fileName
* @return
* @throws Exception
*/
public ObjectStat getStat(String fileName) throws Exception {
ObjectStat stat = minioClient.statObject(StatObjectArgs.builder()
.bucket(bucket).object(fileName).build());
return stat;
}
/**
*上传⽂件到桶中
* @param multipartFile
* @param directory should be end with / or ""
* @return
* @throws IOException
* @throws InvalidKeyException
* @throws ErrorResponseException
* @throws IllegalArgumentException
* @throws InsufficientDataException
* @throws InternalException
* @throws InvalidBucketNameException
* @throws InvalidResponseException
* @throws NoSuchAlgorithmException
* @throws XmlParserException
* @throws RegionConflictException
* @throws ServerException
*/
public String putObject(MultipartFile multipartFile, String directory) throws
IOException, InvalidKeyException, ErrorResponseException, IllegalArgumentException,
InsufficientDataException, InternalException, InvalidBucketNameException, InvalidResponseException,        NoSuchAlgorithmException, XmlParserException, RegionConflictException, ServerException {
log.debug("start upload file .");
String fileName = OriginalFilename();
String rename = "";
log.debug("Upload File name is:" + fileName);
if (StringUtils.isNotEmpty(directory)) {
rename = renameFileVersion(directory + "/" + fileName, minioClient);
} else {
rename = renameFileVersion(fileName, minioClient);
}
try (InputStream inputStream = InputStream()) {
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(rename).
ContentType()).
stream(inputStream, Size(), -1).build());
inputStream.close();
return rename;
}
}
/**
* 获取⽂件的InputStream流对象
* @param fileUri format : directory/filename,filename must contains filetype,like xxx.pdf
* @throws IOException
* @throws InvalidKeyException
* @throws IllegalArgumentException
* @throws NoSuchAlgorithmException
*/
public InputStream getFileInputStream(String fileUri) throws
IOException, InvalidKeyException,IllegalArgumentException, NoSuchAlgorithmException {
log.debug("start get file IO.");
InputStream input=null;
InputStream input=null;
try {
Object(GetObjectArgs.builder().bucket(bucket).object(fileUri).build());
} catch(MinioException e) {
log.debug("Error occurred: " + e.getMessage());
}
return input;
}
/
**
*在桶下创建⽂件夹,⽂件夹层级结构根据参数决定
* @param WotDir should be end with /
* @return
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws InternalException
* @throws XmlParserException
* @throws InvalidBucketNameException
* @throws ErrorResponseException
* @throws RegionConflictException
* @throws ServerException
*/
public String createDirectory(String WotDir) throws IOException, InvalidKeyException,
InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException,
InternalException, XmlParserException, InvalidBucketNameException,
ErrorResponseException, RegionConflictException, ServerException {
log.debug("start create directory.");
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(WotDir).stream(
new ByteArrayInputStream(new byte[] {}), 0, -1)
.build());
log.debug("Create a new Directory:"+WotDir);
return WotDir;
}
/**
* 获取⽂件的下载url
* @param fileUri
* @return
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws ServerException
* @throws InternalException
* @throws XmlParserException
* @throws InvalidBucketNameException
* @throws ErrorResponseException
* @throws RegionConflictException
* @throws InvalidExpiresRangeException
*/
public String getDownloadUrl(String fileUri) throws IOException, InvalidKeyException,
InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException,
ServerException,InternalException, XmlParserException, InvalidBucketNameException,
ErrorResponseException, RegionConflictException,
InvalidExpiresRangeException {
log.debug("start get url for download.");
//Use Get Method
String fileurl = PresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(this.bucket).object(fileUri).method(GET).build());        log.debug("You can download file through this url:"+fileurl);
return fileurl;
}
/**
*该⽅法⽤于更新⽂档,
* @param multipartFile
* @param orderNum
* @return 是Minio中的Object名
* @throws IOException
* @throws InvalidKeyException
* @throws ErrorResponseException
* @throws IllegalArgumentException
* @throws InsufficientDataException
* @throws InternalException
* @throws InvalidBucketNameException
* @throws InvalidResponseException
* @throws NoSuchAlgorithmException
* @throws XmlParserException
* @throws RegionConflictException
* @throws ServerException
*/
public String uploadFileWithArgsForApp(MultipartFile multipartFile,String orderNum,String fileName) throws
IOException, InvalidKeyException, ErrorResponseException, IllegalArgumentException,
InsufficientDataException, InternalException, InvalidBucketNameException, InvalidResponseException,
NoSuchAlgorithmException, XmlParserException, RegionConflictException, ServerException {
log.debug("start upload file .");
log.debug("Upload File name is:"+fileName);
String rename=orderNum+"/"+fileName;
try (InputStream inputStream = InputStream()) {
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(rename).
ContentType()).
stream(Size(),-1).build());
return rename;
}
}
public String getloadUrl(String fileUri) throws IOException, InvalidKeyException,
InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException,
ServerException,InternalException, XmlParserException, InvalidBucketNameException,
ErrorResponseException, RegionConflictException,
InvalidExpiresRangeException {
log.debug("start get url for download.");
springboot结构
//Use Get Method
String fileurl = ObjectUrl(bucket,fileUri);
log.debug("You can download file through this url:"+fileurl);
return fileurl;
}
/**
* 批量下载
* @param directory
* @return
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws ServerException
* @throws InternalException
* @throws XmlParserException
* @throws InvalidBucketNameException
* @throws ErrorResponseException
* @throws InvalidExpiresRangeException
*/
public List<String> downLoadMore(String directory) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerExce        Iterable<Result<Item>> objs = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).prefix(directory).useUrlEncodingType(false).build());
List<String> list =new ArrayList<>();
for (Result<Item> result : objs) {
String objectName = null;
objectName = ().objectName();
ObjectStat statObject = minioClient.statObject(StatObjectArgs.builder().bucket(bucket).object(objectName).build());
if (statObject != null && statObject.length() > 0) {
String fileurl = PresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(this.bucket).object(statObject.name()).method(GET).build());
list.add(fileurl);
}
}
return list;
}
/**
* 刪除⽂件
* @param fileUri
* @return
* @throws IOException
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws ServerException
* @throws InternalException
* @throws XmlParserException
* @throws InvalidBucketNameException
* @throws ErrorResponseException
*/
public  String removeFile(String fileUri) throws IOException, InvalidKeyException, InvalidResponseException,
InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
InvalidBucketNameException, ErrorResponseException {
log.debug("Start remove File :"+fileUri);
ObjectStat objectStat = minioClient.statObject(StatObjectArgs.builder().bucket(bucket).object(fileUri).build());
log.debug("File has been removed");
return fileUri;
}
/**
* 合并PDF
* @param files
* @param dir
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws IOException
* @throws InsufficientDataException
* @throws InternalException
* @throws InvalidResponseException
* @throws InvalidBucketNameException
* @throws XmlParserException
* @throws ServerException
* @throws ErrorResponseException
*/
public String mergeFile(List<String> files, String dir,String name) throws NoSuchAlgorithmException, InvalidKeyException, IOException, InsufficientDataException, InternalException, InvalidR        OutputStream outputStream = new ByteArrayOutputStream();
PDFMergerUtility merger = new PDFMergerUtility();
merger.setDestinationStream(outputStream);
for (String s:files
) {
InputStream in = FileInputStream(s);
merger.addSource(in);//添加所有⽂件的输⼊流对象
}
log.debug("Merge File Stream success" );
ByteArrayOutputStream baos = (ByteArrayOutputStream) DestinationStream();
ByteArrayInputStream swapStream = new ByteArray());
MultipartFile multipartFile = new MockMultipartFile(name+".pdf", name+".pdf", "application/pdf", ByteArray(swapStream));
log.debug("Merge upload File has been create" );
String fileName = OriginalFilename();
String rename = dir+"/"+fileName;
try (InputStream inputStream = InputStream()) {
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(rename).
ContentType()).
stream(inputStream, Size(), -1).build());
inputStream.close();
return rename;
}
}
/**
* 重命名
* @param objectName
* @param minioClient
* @return
* @throws IOException
* @throws InvalidKeyException
* @throws InvalidResponseException
* @throws InsufficientDataException
* @throws NoSuchAlgorithmException
* @throws ServerException

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。