Java教程

minio存储桶

本文主要是介绍minio存储桶,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

添加minio依赖 pom.xml

<!--Minio-->
    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.2.1</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okio</groupId>
        <artifactId>okio</artifactId>
        <version>2.8.0</version>
    </dependency>
    <!--Minio-->

配置minio存储桶 yml

minio:
  endpoint: 域名
  access-key: minio登录名
  secret-key: minio密码
  bucket: minio使用的存储桶名

Minio工具类

JAVA包里新建minio工具类

@Component
@Slf4j
public class MinioUtil {
    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.access-key}")
    private String accessKey;

    @Value("${minio.secret-key}")
    private String secretKey;

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

    private MinioClient client;

    @PostConstruct
    public void init() {
        this.client = new MinioClient.Builder().endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    // 上传图片
    public void uploadImage(String path, MultipartFile file) {
        try {
            //在Minio中保存图片(文件不能超过5M)
            this.client.putObject(PutObjectArgs.builder().bucket(bucket).object(path)
                    .stream(file.getInputStream(), -1, 5 * 1024 * 1024)
                    .contentType("image/jpeg").build());
            log.debug("向" + path + "保存了文件");
        } catch (Exception e) {
            log.error("保存文件失败", e);
            throw new HisException("保存文件失败");
        }
    }

    // 上传excel文件
    public void uploadExcel(String path, MultipartFile file) {
        try {
            //Excel文件的MIME类型
            String mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //Excel文件不能超过20M
            this.client.putObject(PutObjectArgs.builder()
                    .bucket(bucket).object(path)
                    .stream(file.getInputStream(), -1, 20 * 1024 * 1024)
                    .contentType(mime).build());
            log.debug("向" + path + "保存了文件");
        } catch (Exception e) {
            log.error("保存文件失败", e);
            throw new HisException("保存文件失败");
        }
    }

    // 下载文件
    public InputStream downloadFile(String path) {
        try {
            GetObjectArgs args = GetObjectArgs.builder()
                    .bucket(bucket)
                    .object(path)
                    .build();
            return client.getObject(args);
        } catch (Exception e) {
            log.error("文件下载失败", e);
            throw new HisException("文件下载失败");
        }
    }

    // 删除存储桶里面文件的方法
    public void deleteFile(String path) {
        try {
            this.client.removeObject(RemoveObjectArgs.builder()
                    .bucket(bucket)
                    .object(path)
                    .build());
            log.debug("删除了" + path + "路径下的文件");
        } catch (Exception e) {
            log.error("文件删除失败", e);
            throw new HisException("文件删除失败");
        }
    }
}

使用
minioUtil.uploadImage(path, file);
minioUtil.uploadExcel(path, file);
minioUtil.deleteFile(path);

这篇关于minio存储桶的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!