Spring Boot 整合 minio(一步到位)
阅读原文时间:2023年07月08日阅读:3

按照这个步骤来,宝贝保你一步到位

新版本安装

# docker 下载镜像
docker pull minio/minio

安装镜像

docker run \
--name minio \
-p 9000:9000 \
-p 9090:9090 \
-d --restart=always \
-e "MINIO_ROOT_USER=minio" \
-e "MINIO_ROOT_PASSWORD=minio123" \
-v /opt/docker_minio/data:/data \
-v /opt/docker_minio/config:/root/.minio \
minio/minio server /data --console-address ":9090" --address ":9000"

查看日志

docker logs minio

注意注意注意

console-address ":9090" 是页面控制台的端口号,而--address ":9000" 是我们代码中要写的端口号(详见下面application.properties配置)

页面地址 http://192.168.159.173:9090/login

老版本安装

# docker 下载镜像
docker pull minio/minio:RELEASE.2021-06-14T01-29-23Z

安装Minio

docker run \
--name minio \
-p 9000:9000 \
-d --restart=always \
-e "MINIO_ACCESS_KEY=admin" \
-e "MINIO_SECRET_KEY=admin123" \
-v /opt/docker_minio/data:/data \
-v /opt/docker_minio/config:/root/.minio \
minio/minio:RELEASE.2021-06-14T01-29-23Z server /data

查看日志

docker logs minio

(第一个依赖是minio的,下面是swagger的)主要是导入minio的就可以了,因为我的controller方法中用到了swagger,所以这里也导入了。


io.minio minio 7.0.2

    <!-- swagger -->  
    <dependency>  
        <groupId>io.springfox</groupId>  
        <artifactId>springfox-swagger2</artifactId>  
        <version>2.9.2</version>  
    </dependency>  
    <dependency>  
        <groupId>io.springfox</groupId>  
        <artifactId>springfox-swagger-ui</artifactId>  
        <version>2.9.2</version>  
    </dependency>  
    <!-- knife4j -->  
    <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->  
    <dependency>  
        <groupId>com.github.xiaoymin</groupId>  
        <artifactId>knife4j-spring-boot-starter</artifactId>  
        <version>2.0.4</version>  
    </dependency>

#minio
minio.bucket=images
minio.host=http://192.168.159.173:9000
minio.url=${minio.host}/${minio.bucket}/
minio.access-key=minio
minio.secret-key=minio123

package com.hmw.demo.config;

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Component
public class MinioHelper implements InitializingBean {

@Value(value = "${minio.bucket}")  
private String bucket;

@Value(value = "${minio.host}")  
private String host;

@Value(value = "${minio.url}")  
private String url;

@Value(value = "${minio.access-key}")  
private String accessKey;

@Value(value = "${minio.secret-key}")  
private String secretKey;

private MinioClient minioClient;

@Override  
public void afterPropertiesSet()  
        throws Exception {  
    Assert.hasText(url, "Minio url 为空");  
    Assert.hasText(accessKey, "Minio accessKey为空");  
    Assert.hasText(secretKey, "Minio secretKey为空");  
    this.minioClient = new MinioClient(this.host, this.accessKey, this.secretKey);  
}

/\*\*  
 \* 上传  
 \*  
 \* @param multipartFile  
 \* @return  
 \* @throws Exception  
 \*/  
public String putObject(MultipartFile multipartFile)  
        throws Exception {  
    // bucket 不存在,创建  
    if (!minioClient.bucketExists(this.bucket)) {  
        minioClient.makeBucket(this.bucket);  
    }  
    try (InputStream inputStream = multipartFile.getInputStream()) {

        // 上传文件的名称  
        String fileName = multipartFile.getOriginalFilename();

        // PutObjectOptions,上传配置(文件大小,内存中文件分片大小)  
        PutObjectOptions putObjectOptions =  
                new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN\_MULTIPART\_SIZE);  
        // 文件的ContentType  
        putObjectOptions.setContentType(multipartFile.getContentType());  
        minioClient.putObject(this.bucket, fileName, inputStream, putObjectOptions);

        // 返回访问路径  
        return this.url + UriUtils.encode(fileName, StandardCharsets.UTF\_8);  
    }  
}

/\*\*  
 \* 列出所有存储桶名称  
 \*  
 \* @return  
 \* @throws Exception  
 \*/  
public List<String> listBucketNames()  
        throws Exception {  
    List<Bucket> bucketList = listBuckets();  
    List<String> bucketListName = new ArrayList<>();  
    for (Bucket bucket : bucketList) {  
        bucketListName.add(bucket.name());  
    }  
    return bucketListName;  
}

/\*\*  
 \* 查看所有桶  
 \*  
 \* @return  
 \* @throws Exception  
 \*/  
public List<Bucket> listBuckets()  
        throws Exception {  
    return minioClient.listBuckets();  
}

/\*\*  
 \* 检查存储桶是否存在  
 \*  
 \* @param bucketName  
 \* @return  
 \* @throws Exception  
 \*/  
public boolean bucketExists(String bucketName)  
        throws Exception {  
    boolean flag = minioClient.bucketExists(bucketName);  
    if (flag) {  
        return true;  
    }  
    return false;  
}

/\*\*  
 \* 创建存储桶  
 \*  
 \* @param bucketName  
 \* @return  
 \* @throws Exception  
 \*/  
public boolean makeBucket(String bucketName)  
        throws Exception {  
    boolean flag = bucketExists(bucketName);  
    if (!flag) {  
        minioClient.makeBucket(bucketName);  
        return true;  
    } else {  
        return false;  
    }  
}

/\*\*  
 \* 删除桶  
 \*  
 \* @param bucketName  
 \* @return  
 \* @throws Exception  
 \*/  
public boolean removeBucket(String bucketName)  
        throws Exception {  
    boolean flag = bucketExists(bucketName);  
    if (flag) {  
        Iterable<Result<Item>> myObjects = listObjects(bucketName);  
        for (Result<Item> result : myObjects) {  
            Item item = result.get();  
            // 有对象文件,则删除失败  
            if (item.size() > 0) {  
                return false;  
            }  
        }  
        // 删除存储桶,注意,只有存储桶为空时才能删除成功。  
        minioClient.removeBucket(bucketName);  
        flag = bucketExists(bucketName);  
        if (!flag) {  
            return true;  
        }

    }  
    return false;  
}

/\*\*  
 \* 列出存储桶中的所有对象  
 \*  
 \* @param bucketName 存储桶名称  
 \* @return  
 \* @throws Exception  
 \*/  
public Iterable<Result<Item>> listObjects(String bucketName)  
        throws Exception {  
    boolean flag = bucketExists(bucketName);  
    if (flag) {  
        return minioClient.listObjects(bucketName);  
    }  
    return null;  
}

/\*\*  
 \* 列出存储桶中的所有对象名称  
 \*  
 \* @param bucketName 存储桶名称  
 \* @return  
 \* @throws Exception  
 \*/  
public List<String> listObjectNames(String bucketName) throws Exception {  
    List<String> listObjectNames = new ArrayList<>();  
    boolean flag = bucketExists(bucketName);  
    if (flag) {  
        Iterable<Result<Item>> myObjects = listObjects(bucketName);  
        for (Result<Item> result : myObjects) {  
            Item item = result.get();  
            listObjectNames.add(item.objectName());  
        }  
    }  
    return listObjectNames;  
}

/\*\*  
 \* 删除一个对象  
 \*  
 \* @param bucketName 存储桶名称  
 \* @param objectName 存储桶里的对象名称  
 \* @throws Exception  
 \*/  
public boolean removeObject(String bucketName, String objectName)  
        throws Exception {  
    boolean flag = bucketExists(bucketName);  
    if (flag) {  
        minioClient.removeObject(bucketName, objectName);  
        return true;  
    }  
    return false;

}

/\*\*  
 \* 文件访问路径  
 \*  
 \* @param bucketName 存储桶名称  
 \* @param objectName 存储桶里的对象名称  
 \* @return  
 \* @throws Exception  
 \*/  
public String getObjectUrl(String bucketName, String objectName) throws Exception {  
    boolean flag = bucketExists(bucketName);  
    String url = "";  
    if (flag) {  
        url = minioClient.getObjectUrl(bucketName, objectName);  
    }  
    return url;  
}

}

config

package com.hmw.demo.controller;

import com.hmw.demo.config.MinioHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/test")
@Api(tags = {"minio测试接口"})
public class MinioController
{

@Autowired  
MinioHelper minioHelper;

@PostMapping("/upload")  
@ApiOperation("上传")  
public Object upload(@RequestParam("file") MultipartFile multipartFile) throws Exception  
{  
    return this.minioHelper.putObject(multipartFile);  
}

@PostMapping("/list")  
@ApiOperation("列出所有存储桶名称")  
public List<String> list() throws Exception  
{  
    return this.minioHelper.listBucketNames();  
}

@PostMapping("/createBucket")  
@ApiOperation("创建桶")  
public boolean createBucket(String bucketName) throws Exception  
{  
    return this.minioHelper.makeBucket(bucketName);  
}

@PostMapping("/deleteBucket")  
@ApiOperation("删除桶")  
public boolean deleteBucket(String bucketName) throws Exception  
{  
    return this.minioHelper.removeBucket(bucketName);  
}

@PostMapping("/listObjectNames")  
@ApiOperation("列出存储桶中的所有对象名称")  
public List<String> listObjectNames(String bucketName) throws Exception  
{  
    return this.minioHelper.listObjectNames(bucketName);  
}

@PostMapping("/removeObject")  
@ApiOperation("删除一个对象")  
@ApiImplicitParams({  
        @ApiImplicitParam(name = "bucketName", value = "存储桶名称", required = true),  
        @ApiImplicitParam(name = "objectName", value = "存储桶里的对象名称", required = true)  
})  
public boolean removeObject(String bucketName, String objectName) throws Exception  
{  
    return this.minioHelper.removeObject(bucketName, objectName);  
}

@PostMapping("/getObjectUrl")  
@ApiOperation("文件访问路径")  
@ApiImplicitParams({  
        @ApiImplicitParam(name = "bucketName", value = "存储桶名称", required = true),  
        @ApiImplicitParam(name = "objectName", value = "存储桶里的对象名称", required = true)  
})  
public String getObjectUrl(String bucketName, String objectName) throws Exception  
{  
    return this.minioHelper.getObjectUrl(bucketName, objectName);  
}  

}

controller测试

注意:如果桶的访问策略设置的是私有的话,那么访问图片就会出现下面这种情况。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器