当前位置:   article > 正文

docker安装minio

docker安装minio

1、安装minio

docker pull minio/minio:RELEASE.2021-06-17T00-10-46Z
  • 1

2、后台运行minio

docker run -d -p 9000:9000 --name minio\
  -e "MINIO_ACCESS_KEY=admin" \
  -e "MINIO_SECRET_KEY=admin" \
  -v /usr/local/minio/data:/data \
  -v /usr/local/minio/config:/root/.minio \
  minio/minio:RELEASE.2021-06-17T00-10-46Z server /data
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3、基础方法

package com.example.radar.service.impl;

import com.example.config.MinioConfig;
import com.example.radar.service.MinioService;
import io.minio.MinioClient;
import io.minio.policy.PolicyType;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author ychao
 * @date 2021/6/19 20:00
 */
@Service("/minioService")
public class MinioServiceImpl implements MinioService {

    @Override
    public Map minioUpload(MultipartFile file, String dirPath) {
        Map<String, String> minioMap = new HashMap<>();
        String filename = file.getOriginalFilename();
        try {
            //创建一个MinIO的Java客户端
            MinioClient minioClient = new MinioClient(MinioConfig.ENDPOINT, MinioConfig.ACCESS_KEY, MinioConfig.SECRET_KEY);
            boolean isExist = minioClient.bucketExists(MinioConfig.BUCKET_NAME);
            //没有桶则创建
            if (!isExist) {
                //创建存储桶并设置只读权限
                minioClient.makeBucket(MinioConfig.BUCKET_NAME);
                minioClient.setBucketPolicy(MinioConfig.BUCKET_NAME, "*.*", PolicyType.READ_ONLY);
            }
            // 设置存储对象名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
            String objectName = dirPath + "/" + uuid + "_" + filename;
            // 使用putObject上传一个文件到存储桶中
            minioClient.putObject(MinioConfig.BUCKET_NAME, objectName, file.getInputStream(), file.getContentType());
            minioMap.put("fileName", filename);
            minioMap.put("fileUrl", MinioConfig.ENDPOINT + "/" + MinioConfig.BUCKET_NAME + "/" + objectName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return minioMap;
    }

    @Override
    public void minioDownload(String fileUrl,String fileName,HttpServletResponse response) {
        OutputStream outputStream = null;
        InputStream inputStream = null;
        if (StringUtils.isBlank(fileUrl)) {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            System.out.println("fileUrl为null文件下载失败");
        }
        try {
            // 拿到文件路径
            String url = fileUrl.split("9000/")[1];
            // 获取文件对象
            inputStream = getMinioObjectInputStream(MinioConfig.BUCKET_NAME, url.substring(url.indexOf("/") + 1));
            response.reset();
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream, outputStream);

        } catch (Exception ex) {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            System.out.println("文件下载失败");
        } finally {
            try {
                if (null != outputStream) {
                    outputStream.close();
                }
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * create by: ychao
     * description: 获取minio文件中的输入流
     * create time: 2021/6/19 22:51
     */
    public InputStream getMinioObjectInputStream(String bucketName, String objectName) {
        try {
            MinioClient minioClient = new MinioClient(MinioConfig.ENDPOINT, MinioConfig.ACCESS_KEY, MinioConfig.SECRET_KEY);
            return minioClient.getObject(bucketName, objectName);
        } catch (Exception e) {
            System.out.println("minio文件输入流获取失败");
        }
        return null;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/400968
推荐阅读
相关标签
  

闽ICP备14008679号