栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合minio工具类--简单上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合minio工具类--简单上传

pom.xml

		
            io.minio
            minio
            8.2.1
        
application.yml
minio: # minio
  endpoint: http://xxx.xxx.xxx.xxx:9000
  accessKey: admin
  secretKey: admin123
  secure: false
  bucketName: test

secure(表示的是是否使用https)

MinioConfig
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinIoConfig {

    @Autowired
    private MinioProperties minioProperties;

    @Bean
    public MinioClient uploadFile() {

        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()).build();

        return minioClient;
    }
}
MinioProperties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {

    private String endpoint;
    private String accessKey;
    private String secretKey;

}
MinIoUtils
import com.nineclock.system.config.MinioProperties;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Configuration
public class MinIoUtils {

    @Value("${minio.bucketName}")
    private String bucketName;

    @Autowired
    private MinioProperties minioProperties;

    @Autowired
    private MinioClient minioClient;
    
    public String uploadFile(String fileName, MultipartFile file) {

        fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + "-" + fileName;

        try {

            InputStream in = file.getInputStream();

            List fileNameList = new ArrayList<>();

            fileNameList.add(fileName);

            minioClient.putObject(
                    PutObjectArgs.builder().bucket(bucketName)  // 桶名
                            .object(fileName)  // 文件
                            .stream(in,file.getSize(),-1)  // 文件输出流
                            .contentType(file.getContentType())  // 文件格式
                            .build());
            String url = minioProperties.getEndpoint() + "/" + bucketName + "/" + fileName;

            log.info("url:" + url); // 前端直传需要的url地址
            return url;
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
        return null;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697259.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号