FileServerOptUtil.java 4.07 KB
package com.pashanhoo.common.minio;

import io.minio.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;

/**
 * @author CAIYONGSONG
 * @commpany www.pashanhoo.com
 * @date 2022/7/20
 */
@Component
public class FileServerOptUtil {
    private Logger log = LoggerFactory.getLogger(FileServerOptUtil.class);

    @Autowired
    MinioClient minioClient;

    /**
     * 获取桶中某个对象的输入流
     * @param bucketName
     * @param path
     * @return
     */
    public InputStream getObjectInputStream(String bucketName, String path){
        log.info("从桶:" + bucketName + ",获取:" + path + "对象流!");
        InputStream stream = null;
        try {
            stream = minioClient.getObject(
                    GetObjectArgs.builder().bucket(bucketName).object(path).build());
        } catch (Exception e) {
            log.error("从桶:" + bucketName + ",获取:" + path + "对象流错误!");
            e.printStackTrace();
        }
        return stream;
    }

    /**
     *
     * @param bucketName
     * @param dirPath 如:path/to/
     * @return
     */
    public  boolean mkdir(String bucketName,String dirPath){
        log.info("在桶:" + bucketName + ",中创建:" + dirPath);
        boolean result = false;
        try {
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(bucketName).object(dirPath).stream(
                            new ByteArrayInputStream(new byte[] {}), 0, -1)
                            .build());
            result = true;
        } catch (Exception e) {
            log.error("在桶:" + bucketName + ",中创建:" + dirPath + "错误!");
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 往对象存储服务目标桶的目标位置上传文件
     * @param bucketName 桶
     * @param toPath  目标位置如:test/2.txt
     * @param fromPath 本地文件位置
     * @return
     */
    public  boolean uploadObject(String bucketName,String toPath,String fromPath ){
        log.info("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath);
        boolean result = false;
        try {
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(toPath)
                            .filename(fromPath)
                            .build());
            result = true;
        } catch (Exception e) {
            log.error("从本地:" + fromPath + "往桶:" + bucketName + ",中上传文件:" + toPath + "错误!");
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 从桶中下载文件到本地文件
     * @param bucketName
     * @param fromPath
     * @param toPath
     * @return
     */
    public  boolean downloadFile(String bucketName,String fromPath,String toPath){
        log.info("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath);
        boolean result = false;
        try {
            minioClient.downloadObject(
                    DownloadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fromPath)
                            .filename(toPath)
                            .build());
            result = true;
        } catch (Exception e) {
            log.error("从桶:" + bucketName + "中将" + fromPath + "文件下载到:" + toPath + "错误!");
            e.printStackTrace();
        }
        return result;
    }
    public static String extractFilename(MultipartFile file){
        if (file.isEmpty()) {
            throw new RuntimeException("文件不存在!");
        }
        return DateUtils.dateToStr(new Date()) + "/" + System.currentTimeMillis() + "_" + file.getOriginalFilename()  ;

    }
}