minio | docker

2023-10-25 08:20:12 浏览数 (1)

拉取

代码语言:javascript复制
docker pull minio/minio

启动

代码语言:javascript复制
docker run  -p 8028:8028 -p 9000:9000 --name minio 
 -d --restart=always 
 -e MINIO_ACCESS_KEY=minioadmin 
 -e MINIO_SECRET_KEY=minioadmin 
 -v /usr/local/minio/data:/data 
 -v /usr/local/minio/config:/root/.minio 
  minio/minio server /data  --console-address ":8028" --address ":9000"

创建key

java

代码语言:yaml复制
minio:
  #地址
  endpoint: http://xx.xx.xx.xx:9000
  # 桶名称
  bucketName: bucket-dev
  # access-key
  accessKey: qQmdd73Q4ZCoMrYtq4do
  # secret-key
  secretKey: AZoO2S1gAasdqweqweqwezxc3xYip683jpMByd

MinioUtils

代码语言:java复制
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author xuyt
 */
@Slf4j
@Component
public class MinioUtils {

    @Resource
    private MinioClient minioClient;

    /**
     * 判断桶是否存在
     *
     * @param bucketName 桶名称
     * @return 是否存在
     */
    @SneakyThrows(Exception.class)
    public boolean bucketNotExists(String bucketName) {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }

    /**
     * 创建桶
     *
     * @param bucketName 桶名称
     */
    @SneakyThrows(Exception.class)
    public void createBucket(String bucketName) {
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 根据bucketName 获取信息
     *
     * @param bucketName 桶名称
     * @return 获取桶信息
     */
    @SneakyThrows(Exception.class)
    public Optional<Bucket> getBucket(String bucketName) {
        return minioClient.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
    }

    /**
     * 根据bucketName删除信息
     *
     * @param bucketName 桶名称
     */
    @SneakyThrows(Exception.class)
    public void removeBucket(String bucketName) {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 获取文件流
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @return 二进制流
     */
    @SneakyThrows(Exception.class)
    public InputStream getObject(String bucketName, String objectName) {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 上传本地文件
     *
     * @param bucketName 桶名称
     * @param objectName 对象名称
     * @param fileName   本地文件路径
     * @return response
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse putObject(String bucketName, String objectName, String fileName) {
        if (bucketNotExists(bucketName)) {
            createBucket(bucketName);
        }
        return minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());
    }

    /**
     * 通过流上传文件
     *
     * @param bucketName  桶名称
     * @param objectName  文件对象
     * @param inputStream 文件流
     * @return response
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse putObject(String bucketName, String objectName, InputStream inputStream) {
        if (bucketNotExists(bucketName)) {
            createBucket(bucketName);
        }
        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, inputStream.available(), -1).build());
    }

    /**
     * 单个文件上传
     *
     * @param bucketName    桶名称
     * @param multipartFile 文件
     * @return 地址
     */
    public String uploadFileSingle(String bucketName, MultipartFile multipartFile) {
        if (bucketNotExists(bucketName)) {
            createBucket(bucketName);
        }
        String fileMinioName = "";
        String fileName = multipartFile.getOriginalFilename();
        String[] split = fileName.split("\.");
        if (split.length > 1) {
            fileName = split[0]   "_"   System.currentTimeMillis()   "."   split[1];
        } else {
            fileName = fileName   System.currentTimeMillis();
        }
        InputStream in = null;
        try {
            in = multipartFile.getInputStream();
            ObjectWriteResponse response = minioClient.putObject(
                    PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(in, in.available(), -1).contentType(multipartFile.getContentType()).build());
            fileMinioName = response.object();
        } catch (Exception e) {
            log.error("minio上传单个文件出错", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error("minio上传单个文件,关闭文件流出错", e);
                }
            }
        }
        return fileMinioName;
    }

    /**
     * description: 上传文件
     *
     * @param multipartFile 文件
     * @return java.lang.String
     */
    public List<String> uploadFileBatch(String bucketName, MultipartFile[] multipartFile) {
        if (bucketNotExists(bucketName)) {
            createBucket(bucketName);
        }
        List<String> names = new ArrayList<>();
        for (MultipartFile file : multipartFile) {
            try {
                String fileName = file.getOriginalFilename();
                uploadFileSingle(bucketName, file);
                names.add(fileName);
            } catch (Exception e) {
                log.error("minio上传多个文件出错", e);
            }
        }
        return names;
    }


    /**
     * 获取文件外链
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @param expires    过期时间 <=7 秒级
     * @return url
     */
    @SneakyThrows(Exception.class)
    public String getUploadObjectUrl(String bucketName, String objectName, Integer expires) {
        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.PUT).bucket(bucketName).object(objectName).expiry(expires).build());
    }

    /**
     * 下载文件
     * bucketName:桶名
     *
     * @param fileName: 文件名
     */
    @SneakyThrows(Exception.class)
    public void download(String bucketName, String fileName, HttpServletResponse response) {
        // 获取对象的元数据
        StatObjectResponse stat = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileName).build());
        response.setContentType(stat.contentType());
        response.setCharacterEncoding("UTF-8");

        response.setHeader("Content-Disposition", "attachment;filename="   URLEncoder.encode(fileName, "UTF-8"));
        InputStream is = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
        IOUtils.copy(is, response.getOutputStream());
        is.close();
    }

    /**
     * 获取文件详情
     *
     * @param bucketName 桶名称
     * @param fileName   文件名称
     * @return 文件详情
     */
    @SneakyThrows(Exception.class)
    public StatObjectResponse getFileInfo(String bucketName, String fileName) {
        return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileName).build());
    }

    /**
     * 删除文件
     *
     * @param bucketName 桶名称
     * @param name       文件对象
     */
    @SneakyThrows(Exception.class)
    public void removeObject(String bucketName, String name) {
        if (bucketNotExists(bucketName)) {
            createBucket(bucketName);
        }
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(name).build());
    }
}

0 人点赞