123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- package com.fdkankan.fusion.common.util;
- import com.aliyun.oss.OSSClient;
- import com.aliyun.oss.model.GetObjectRequest;
- import com.fdkankan.fusion.config.MinIOConfig;
- import com.fdkankan.fusion.response.FileInfoVo;
- import io.minio.*;
- import io.minio.messages.Item;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.util.ObjectUtils;
- import javax.annotation.PostConstruct;
- import javax.annotation.Resource;
- import java.io.InputStream;
- import java.time.ZonedDateTime;
- import java.util.Date;
- import java.util.List;
- @Slf4j
- @Service
- public class MinIoUploadService {
- @Resource
- private MinIOConfig minIOConfig;
- private MinioClient minioClient;
- private String endpoint;
- private String bucketName;
- private String accessKey;
- private String secretKey;
- private Integer imgSize;
- private Integer fileSize;
- private final String SEPARATOR = "/";
- @PostConstruct
- public void init() {
- this.endpoint = minIOConfig.getEndpoint();
- this.bucketName = minIOConfig.getBucketName();
- this.accessKey = minIOConfig.getAccessKey();
- this.secretKey = minIOConfig.getSecretKey();
- createMinioClient();
- }
- /**
- * 创建基于Java端的MinioClient
- */
- public void createMinioClient() {
- try {
- if (null == minioClient) {
- log.info("开始创建 MinioClient...");
- minioClient = MinioClient
- .builder()
- .endpoint(endpoint)
- .credentials(accessKey, secretKey)
- .build();
- createBucket(bucketName);
- log.info("创建完毕 MinioClient...");
- }
- } catch (Exception e) {
- log.error("MinIO服务器异常:{}", e);
- }
- }
- /**
- * 启动SpringBoot容器的时候初始化Bucket
- * 如果没有Bucket则创建
- *
- * @throws Exception
- */
- private void createBucket(String bucketName) throws Exception {
- if (!bucketExists(bucketName)) {
- minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
- }
- }
- /**
- * 判断Bucket是否存在,true:存在,false:不存在
- *
- * @return
- * @throws Exception
- */
- public boolean bucketExists(String bucketName) throws Exception {
- return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
- }
- /**
- * 获得Bucket的策略
- *
- * @param bucketName
- * @return
- * @throws Exception
- */
- public String getBucketPolicy(String bucketName) throws Exception {
- String bucketPolicy = minioClient
- .getBucketPolicy(
- GetBucketPolicyArgs
- .builder()
- .bucket(bucketName)
- .build()
- );
- return bucketPolicy;
- }
- /**
- * 判断文件是否存在
- *
- * @param bucketName 存储桶
- * @param objectName 文件名
- * @return
- */
- public boolean isObjectExist(String bucketName, String objectName) {
- boolean exist = true;
- try {
- minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
- } catch (Exception e) {
- exist = false;
- }
- return exist;
- }
- public boolean isObjectExist(String objectName) {
- return isObjectExist(bucketName,objectName);
- }
- /**
- * 判断文件夹是否存在
- *
- * @param bucketName 存储桶
- * @param objectName 文件夹名称
- * @return
- */
- public boolean isFolderExist(String bucketName, String objectName) {
- boolean exist = false;
- try {
- Iterable<Result<Item>> results = minioClient.listObjects(
- ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());
- for (Result<Item> result : results) {
- Item item = result.get();
- if (item.isDir() && objectName.equals(item.objectName())) {
- exist = true;
- }
- }
- } catch (Exception e) {
- exist = false;
- }
- return exist;
- }
- public boolean isFolderExist( String objectName) {
- return isFolderExist(bucketName,objectName);
- }
- /**
- * 获取路径下文件列表
- *
- * @param bucketName 存储桶
- * @param prefix 文件名称
- * @param recursive 是否递归查找,false:模拟文件夹结构查找
- * @return 二进制流
- */
- public Iterable<Result<Item>> listObjects(String bucketName, String prefix,
- boolean recursive) {
- return minioClient.listObjects(
- ListObjectsArgs.builder()
- .bucket(bucketName)
- .prefix(prefix)
- .recursive(recursive)
- .build());
- }
- /**
- * 获取文件流
- *
- * @param bucketName 存储桶
- * @param objectName 文件名
- * @return 二进制流
- */
- public InputStream getObject(String bucketName, String objectName) throws Exception {
- return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
- }
- public InputStream getObject( String objectName) {
- try {
- return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
- }catch (Exception e){
- log.info("minio获取文件失败:{}",objectName,e);
- }
- return null;
- }
- /**
- * 获取文件信息, 如果抛出异常则说明文件不存在
- *
- * @param bucketName 存储桶
- * @param objectName 文件名称
- */
- public FileInfoVo getFileStatusInfo(String bucketName, String objectName) throws Exception {
- StatObjectResponse statObjectResponse = minioClient.statObject(
- StatObjectArgs.builder()
- .bucket(bucketName)
- .object(objectName)
- .build());
- String md5 = statObjectResponse.etag().toUpperCase();
- ZonedDateTime zonedDateTime = statObjectResponse.lastModified();
- Date LastMo = Date.from(zonedDateTime.toInstant());
- Long size = statObjectResponse.size();
- InputStream inputStream = getObject(bucketName, objectName);
- String sha1 = MD5Checksum.getSHA1(inputStream);
- return new FileInfoVo(md5,sha1.toUpperCase(),LastMo.getTime(),size);
- }
- public FileInfoVo getFileStatusInfo( String objectName) {
- try {
- return getFileStatusInfo(bucketName,objectName);
- }catch (Exception e){
- log.info("获取文件失败或文件不存在:{},{}",bucketName,objectName,e);
- }
- return null;
- }
- public Long getSize(String filePath){
- Long total = 0L;
- try {
- Iterable<Result<Item>> results = listObjects(bucketName, filePath, true);
- if (ObjectUtils.isEmpty(results)) {
- return 0L;
- }
- for (Result<Item> result : results) {
- Item item = result.get();
- long size = item.size();
- total += size;
- }
- }catch (Exception e){
- log.info("oss-getFileInfo-error:{}",e);
- }
- return total;
- }
- }
|