首页 > 其他分享 >minio-docker

minio-docker

时间:2024-09-03 18:14:05浏览次数:8  
标签:minio param inputStream org import docker String

docker-compose

version: "3"
services:
  minio:
    image: minio/minio:latest
    container_name: minio
    restart: always
    ports:
      - "9000:9000"                                    
      - "9090:9090"                                    
    volumes:                                             
      - "./data:/data"
      - "./config:/root/.minio"
      - /etc/localtime:/etc/localtime:ro
    command: server /data --console-address ":9090" -address ":9000"   
    environment:
      MINIO_ROOT_USER: admin
      MINIO_ROOT_PASSWORD: admin123

还可选择其他镜像

minio/minio              RELEASE.2023-10-14T05-17-22Z.fips
minio/minio              RELEASE.2022-11-29T23-40-49Z 
minio/minio              RELEASE.2021-06-17T00-10-46Z 

使用方法都一致

可出现的问题

将桶设置为public

就是在web页面上无法设置 bucket 为 public

这个经过本人大量验证,就是网络问题,被服务器端拦截或断开;

解决方案:登录的minio所安装的服务器上执行以下命令

curl -X PUT 
http://127.0.0.1:9090/api/v1/buckets/XXXXXX/set-policy      ## ip端口还有中间路径里面的XXXXX是桶名,记得修改
-H "Content-Type:application/json" 
-H "Cookie:token=AL0GUGqYRhLcuy82AN7bmc39nYkhhLY0aH2OIk+wEOywuS3TM2u5BaXAWaTYP7wVEUEuu3C23YcvGkoyFyqEwAsYXtQjrWL+Cal+tY5iJ0etGfz+9kCi+wivn9Lhp8WZZFuc7jrH7tziDRFJbrDcGeyNHWuoz16OKNUBD9mdPpdIDb7ARdjEds8rIhvWewUjRboBJSwBRWjsoVGmTWreNfuSscUaw7HGhgZQ29GBFX4+sVr7Nby3Rgkf0hBqLL97k3qhkSh6nL0f1znNO3DPVmefpVmKpGM69n7lft6r6su8DnPkMI2yc90/Nl85BpXF/DmFgM9Hr1KM6Vdtuc9CfWQdXhaaDXcBfh3TXvRUbel5T0lEmX4xMBDsjlZVExhQZUEZs3LpmmwiFtw+OsHdrC7htgOxoqeIzuwOZbY5tTNSpsioOM5UdItfnknWorbhgWKPtXcqmGbrSeVIxFEL6VIlx0Qy4Az/r+lUI5Fco1cUCSnzPXVtutEVl7+UnruXdr5OcEksfljJSfGu8SART5WlJ7Op5dcKxIHwmYQCzpY="              ## cookie的内容记得修改,浏览器上有
-d '{"access": "PUBLIC","definition": "{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n {\n   \"Effect\": \"Allow\",\n  \"Principal\": {\n   \"AWS\": [\n   \"*\"\n  ]\n  },\n  \"Action\": [\n  \"s3:GetBucketLocation\",\n \"s3:ListBucket\",\n  \"s3:ListBucketMultipartUploads\"\n            ],\n            \"Resource\": [\n                \"arn:aws:s3:::XXXXX\"\n            ]\n        },\n        {\n            \"Effect\": \"Allow\",\n            \"Principal\": {\n                \"AWS\": [\n                    \"*\"\n                ]\n            },\n            \"Action\": [\n                \"s3:AbortMultipartUpload\",\n                \"s3:DeleteObject\",\n                \"s3:GetObject\",\n                \"s3:ListMultipartUploadParts\",\n                \"s3:PutObject\"\n            ],\n            \"Resource\": [\n                \"arn:aws:s3:::XXXXX/*\"\n            ]\n        }\n    ]\n}"}'    ## 参数里面的 XXXXX 桶名记得修改

nginx配置


# minio文件服务,具体路径自己配置
    location /minio/ {
        proxy_pass   http://XXXX:9000/;
	proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_connect_timeout 300;

    proxy_http_version 1.1;
    proxy_set_header Connection "";
    chunked_transfer_encoding off;
    }

Java代码

<dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>8.1.0</version>
        </dependency>
minio:
  # MinIO服务器地址
  endpoint: http://XXXX:9000
  # MinIO服务器访问凭据
  accessKey: admin
  secretKey: admin123
  # MinIO桶名称
  bucket: 自己设置的桶名
  # MinIO读取路径前缀,内网和外网
  readPathNw: http://XXXXXX:9000
  readPathWw: https://XXXX


import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;  
  
  
@Data  
@Configuration  
@EnableConfigurationProperties({MinIOConfigProperties.class})  
//当引入FileStorageService接口时
@ConditionalOnClass(IMinioService.class)
public class MinIOConfig {
  
    @Autowired  
    private MinIOConfigProperties minIOConfigProperties;  
  
    @Bean  
    public MinioClient buildMinioClient() {  
        return MinioClient  
                .builder()  
                .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())  
                .endpoint(minIOConfigProperties.getEndpoint())  
                .build();  
    }  
}

  
  
import lombok.Data;  
import org.springframework.boot.context.properties.ConfigurationProperties;  
  
import java.io.Serializable;  
  
@Data  
@ConfigurationProperties(prefix = "minio")  // 文件上传 配置前缀file.oss  
public class MinIOConfigProperties implements Serializable {  
  
    private String accessKey;  
    private String secretKey;  
    private String bucket;  
    private String endpoint;  
    private String readPathNw;
    private String readPathWw;
}



import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @title MinioServiceImpl
 * @description minio服务
 * @since 2024/9/3 16:54
 */
@Slf4j
@EnableConfigurationProperties(MinIOConfigProperties.class)
@Import(MinIOConfig.class)
@Component
public class MinioServiceImpl implements IMinioService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinIOConfigProperties minIOConfigProperties;

    private final static String separator = "/";

    /**
     * @param dirPath
     * @param filename  yyyy/mm/dd/file.jpg
     * @return
     */
    public String builderFilePath(String dirPath,String filename) {
        StringBuilder stringBuilder = new StringBuilder(50);
        if(!StringUtils.isEmpty(dirPath)){
            stringBuilder.append(dirPath).append(separator);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String todayStr = sdf.format(new Date());
        stringBuilder.append(todayStr).append(separator);
        stringBuilder.append(filename);
        return stringBuilder.toString();
    }

    /**
     *  上传文件
     * @param prefix  文件前缀
     * @param filename  文件名
     * @param inputStream 文件流
     * @return  文件全路径
     */
    @Override
    public Map<String, String> uploadFile(String prefix, String filename, InputStream inputStream) {
        Map<String, String> map = new HashMap<>(2);
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)
//                    .contentType("image/jpg")
                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream,inputStream.available(),-1)
                    .build();
            minioClient.putObject(putObjectArgs);
            // 内网地址
            StringBuilder nwUrlPath = new StringBuilder(minIOConfigProperties.getReadPathNw());
            nwUrlPath.append(separator+minIOConfigProperties.getBucket()).append(separator).append(filePath);
            // 外网地址
            StringBuilder wwUrlPath = new StringBuilder(minIOConfigProperties.getReadPathWw());
            wwUrlPath.append(separator+minIOConfigProperties.getBucket()).append(separator).append(filePath);
            map.put("nw", nwUrlPath.toString());
            map.put("ww", wwUrlPath.toString());
            return map;
        }catch (Exception ex){
            log.error("minio put file error.",ex);
            throw new RuntimeException("上传文件失败");
        }
    }

    /**
     *  上传html文件
     * @param prefix  文件前缀
     * @param filename   文件名
     * @param inputStream  文件流
     * @return  文件全路径
     */
    @Override
    public String uploadHtmlFile(String prefix, String filename,InputStream inputStream) {
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)
                    .contentType("text/html")
                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream,inputStream.available(),-1)
                    .build();
            minioClient.putObject(putObjectArgs);
            StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPathNw());
            urlPath.append(separator+minIOConfigProperties.getBucket());
            urlPath.append(separator);
            urlPath.append(filePath);
            return urlPath.toString();
        }catch (Exception ex){
            log.error("minio put file error.",ex);
            ex.printStackTrace();
            throw new RuntimeException("上传文件失败");
        }
    }

    /**
     * 删除文件
     * @param pathUrl  文件全路径
     */
    @Override
    public void delete(String pathUrl) {
        String key = pathUrl.replace(minIOConfigProperties.getEndpoint()+"/","");
        int index = key.indexOf(separator);
        String bucket = key.substring(0,index);
        String filePath = key.substring(index+1);
        // 删除Objects
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(filePath).build();
        try {
            minioClient.removeObject(removeObjectArgs);
        } catch (Exception e) {
            log.error("minio remove file error.  pathUrl:{}",pathUrl);
            e.printStackTrace();
        }
    }


    /**
     * 下载文件
     * @param pathUrl  文件全路径
     * @return  文件流
     *
     */
    @Override
    public byte[] downLoadFile(String pathUrl)  {
        String key = pathUrl.replace(minIOConfigProperties.getEndpoint()+"/","");
        int index = key.indexOf(separator);
        String bucket = key.substring(0,index);
        String filePath = key.substring(index+1);
        InputStream inputStream = null;
        try {
            inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());
        } catch (Exception e) {
            log.error("minio down file error.  pathUrl:{}",pathUrl);
            e.printStackTrace();
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while (true) {
            try {
                if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            byteArrayOutputStream.write(buff, 0, rc);
        }
        return byteArrayOutputStream.toByteArray();
    }
}


import java.io.InputStream;
import java.util.Map;

/**
 * @title IMinioService
 * @description minio服务
 * @since 2024/9/3 16:53
 */
public interface IMinioService {


    /**
     *  上传图片文件
     * @param prefix  文件前缀
     * @param filename  文件名
     * @param inputStream 文件流
     * @return  文件全路径
     */
    Map<String, String> uploadFile(String prefix, String filename, InputStream inputStream);

    /**
     *  上传html文件
     * @param prefix  文件前缀
     * @param filename   文件名
     * @param inputStream  文件流
     * @return  文件全路径
     */
    String uploadHtmlFile(String prefix, String filename,InputStream inputStream);

    /**
     * 删除文件
     * @param pathUrl  文件全路径
     */
    void delete(String pathUrl);

    /**
     * 下载文件
     * @param pathUrl  文件全路径
     * @return
     *
     */
    byte[]  downLoadFile(String pathUrl);

}


import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 文件管理类
 * </p>
 *
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/file")
public class FileManageController {

    @Autowired
    private IMinioService service;
    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/uploadOneFile")
    public Map<String, String> uploadFile(@RequestParam("file") MultipartFile multipartFile) {
        try(InputStream inputStream = multipartFile.getInputStream()) {  // 将MultipartFile转换为InputStream
            // 上传到MinIO服务器
            // 这里的文件名可以生成随机的名称,防止重复
            // 获取真实文件名
            String originalFilename = multipartFile.getOriginalFilename();
            // 使用UUID作为文件名,获取后缀
            StringBuffer name = new StringBuffer(UUID.randomUUID().toString().replaceAll("-", ""));
            if(StringUtils.isNotBlank(originalFilename) && originalFilename.contains(".")){
                name.append(".").append(originalFilename.substring(originalFilename.lastIndexOf(".") + 1));
            }
            return service.uploadFile("第一级的文件目录名", String.valueOf(name), inputStream);
        } catch (IOException e) {
            // 处理异常,可能是getInputStream()失败
            return null;
        }

    }

}

标签:minio,param,inputStream,org,import,docker,String
From: https://www.cnblogs.com/xy20211005/p/18395146

相关文章

  • Docker 常用命令
    镜像管理列出镜像dockerimages拉取镜像dockerpull<image>删除镜像dockerrmi<image>构建镜像dockerbuild-t<tag><path>容器管理列出运行中的容器dockerps列出所有容器(包括停止的)dockerps-a运行容器dockerrun[options]<image>......
  • Docker常用网路介绍
    说明Docker网络模式配置说明host模式–net=host容器和宿主机共享Networknamespace。container模式–net=container:NAME_or_ID容器和另外一个容器共享Networknamespace。kubernetes中的pod就是多个容器共享一个Networknamespace。none模式......
  • Docker内部组件介绍
    Docker的内部组件主要包括:DockerDaemon(dockerd):管理容器和镜像,处理DockerAPI请求。DockerClient(docker):与DockerDaemon交互的命令行工具。DockerImages:用于创建容器的不可变模板。DockerContainers:从镜像创建的运行实例。DockerRegistry:存储......
  • MINIO部署
    创建挂载目录mkdir-p/root/dockerData/ivan_minio/启动方式docker-compose方式启动创建docker-compose.yml文件vimdocker-compose.ymlversion:'3'services:minio:image:"quay.io/minio/minio:RELEASE.2022-08-02T23-59-16Z"container_name:mini......
  • 四款主流 Docker 可视化工具,免费又好用 - 推荐使用朵云
    前言Docker提供了命令行工具来管理Docker的镜像和运行Docker的容器。我们也可以使用图形工具来管理Docker。目前,主流的Docker图形工具有DockerClouds、DockerUl、Portainer和Shipyard。DockerClouds朵云DockerClouds朵云是一款最简单的,单机环境中的管理Docke......
  • CentOS 7 docker 部署遇到内网通,外网不同 问题
     问题  [root@localhost~]#systemctlstatusnetwork●network.service-LSB:Bringup/downnetworkingLoaded:loaded(/etc/rc.d/init.d/network;bad;vendorpreset:disabled)Active:failed(Result:exit-code)sinceTue2024-09-0314:21:22CST;......
  • flask简单自学(docker形式)
    前言参考详细地址:https://juejin.cn/post/6970663530215407652,感谢作者,作者主页直达正文flask的dockerfile(python版本3.8.6)注意:在docker里面运行python需要指定host和port:app.run(host=‘0.0.0.0’,port=5000,debug=True)requirements.txt依赖项内容:Flaskgunicorn......
  • docker-compose轻松部署jenkins
    一、安装部署docker1、安装docker-ce可以参考:docker-ce镜像_docker-ce下载地址_docker-ce安装教程-阿里巴巴开源镜像站(aliyun.com)https://developer.aliyun.com/mirror/docker-ce?spm=a2c6h.13651102.0.0.3e221b11EZ9mMK2、配置docker镜像加速[root@docker~]#cat/etc......
  • docker常用命令
    1、命令汇总官方命令说明:https://docs.docker.com/engine/reference/commandline/docker/dockerversion#显示docker的版本信息dockerinfo#显示docker的系统信息,包括镜像和容器的数量docker--help#帮助命令docker子命令--h......
  • 在本地通过Docker安装MySQL并配置持久化
    看前须知这里使用Mac下的OrbStack代替Docker,都是命令行操作,除了可能出现的UI不同外没有任何区别。拉取镜像由于是Mac环境,所以优先使用arm64结构,关于是否支持所需架构,可以在DockerHub上查,一般支持的都会写明。这里搜索MySQL,进入官方镜像就可以在Overview的Quickreference(co......