自学内容网 自学内容网

docker安装minio、使用springboot集成minio同时创建并设置minio桶仅可读

docker-compose安装minio,并设置挂载目录

version: '3.8'

services:
  minio:
    image: minio/minio
    container_name: minio
    environment:
      MINIO_ROOT_USER: root
      MINIO_ROOT_PASSWORD: 123456789
    restarts: always
    privileged: true
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - ./minio-data:/data
    command: server /data --console-address ":9001"

其中请自行修改用户名、密码、挂载目录和对应的端口与映射(密码至少需要9位,否则启动报错)

其中9001是后台服务的端口

springboot集成minio

pom.xml

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.2.0</version>
</dependency>

application.yml 

minio:
  endpoint: http://xxx:9000 #Minio服务所在地址
  bucket: test #存储桶名称
  accessKey: root #访问的key
  secretKey: 1234 #访问的秘钥

 springboot读取配置文件并注册bean

配置属性的实体类

@Data
@ConfigurationProperties(prefix = "minio")
@Component
public class MinioProp {
  private String endpoint;
  private String accessKey;
  private String secretKey;
  private String bucket;
}

配置类

@Configuration
public class MinioConfig {
    @Autowired
    private MinioProp minioProp;
    @Bean
    public MinioClient minioClient(){
        return MinioClient.builder()
                .endpoint(minioProp.getEndpoint())
                .credentials(minioProp.getAccessKey(), minioProp.getSecretKey())
                .build();
    }
}

springboot创建桶并设置桶仅可读

以下代码仅做示例

@RestController
@Slf4j
public class TestController {
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioProp minioProp;

    @GetMapping("/init")
    public R<String> init() {
        try {
            boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProp.getBucket()).build());
            if (!exists) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProp.getBucket()).build());
            }

        } catch (Exception e) {
            return R.fail(ResultStatusEnum.FAIL);
        }
        return R.ok("init success");
    }

    @GetMapping("/set-public")
    public R<String> setPublic() {
        String policyJson = "{\n" +
                "    \"Version\": \"2012-10-17\",\n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": {\n" +
                "                \"AWS\": [\n" +
                "                    \"*\"\n" +
                "                ]\n" +
                "            },\n" +
                "            \"Action\": [\n" +
                "                \"s3:GetObject\"\n" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::" + minioProp.getBucket() + "/*\"\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        try {
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(minioProp.getBucket()).config(policyJson).build());
        } catch (Exception e) {
            return R.fail(ResultStatusEnum.FAIL);
        }
        return R.ok("set public success");
    }
}

上传文件

@Autowired
private MinioClient minioClient;
@Autowired
private MinioProp minioProp;
@PostMapping
public R<String> uploadImage(@RequestParam("file") MultipartFile file) {
    try {
        String originalFilename = file.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);
        String path = "testPath";
        String fileName = IdUtil.fastSimpleUUID() + StrUtil.DOT + type;
        minioClient.putObject(PutObjectArgs.builder()
                                      .bucket(minioProp.getBucket())
                                      .object(path+ '/' + fileName)
                                      .contentType(file.getContentType())
                                      .stream(file.getInputStream(), file.getInputStream().available(), -1)
                                      .build());
        log.info("文件上传成功:{}", originalFilename);
        return R.ok(minioProp.getEndpoint() +
                            "/" + minioProp.getBucket() +
                            "/" + path+
                            "/" + fileName);
    } catch (Exception e) {
        log.error("上传文件失败", e);
        return R.fail(ResultStatusEnum.FAIL);
    }
}

此时通过上面方法返回的地址即可访问该文件

通过9001端口进入到后台页面,可以发现该文件已上传


原文地址:https://blog.csdn.net/KyLen_Mou/article/details/143745570

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!