minio的使用学习

minio的使用学习

安装

https://docs.min.io/
http://slack.minio.org.cn/
## 下载地址
## minio 默认端口是9000 确保端口开放或者防火墙关闭
wget  https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
## 创建目录
cd software/
mkdir minio/data -p
## 启动
nohup  ./minio server /software/data/minio/  &
RootUser: minioadmin  用户名
RootPass: minioadmin  密码

## 自定义端口启动
nohup  ./minio server --address 192.168.5.200:9001 /software/data/minio/  &

## 关闭的话,我直接kill 
jobs -l
kill PID  ## 自己替换掉PID

在这里插入图片描述

整合SpringBoot开发

pom引入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

minio的配置

@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {

    private Boolean enable;

    private String ipAddr;

    private Integer port;

    private String username;

    private String password;

    private String bucketName;
}
@Configuration
@ConditionalOnProperty(prefix = "minio", name = "enable", havingValue = "true", matchIfMissing = true)
public class MinioConfig {

    @Autowired
    private MinioProperties minioProperties;

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder().endpoint(minioProperties.getIpAddr(), minioProperties.getPort(), false)
                .credentials(minioProperties.getUsername(), minioProperties.getPassword()).build();
    }
}

自己写的几个service方法,还没研究透彻,有待后续更新

public interface IMinioService {

    /**
     * 检查桶是否存在
     * @param bucket
     * @return
     */
    boolean checkBucketIsExist(String bucket) throws Exception;

    /**
     * 创建桶
     * @param bucket name
     * @return
     */
    boolean createBucket(String bucket) throws Exception;

    /**
     * 上传文件
     * @param file
     * @param fileName
     * @param bucket
     * @return
     */
    String uploadFile(File file, String fileName, String contentType, String bucket) throws Exception;

    String uploadFile(InputStream is, String fileName, String contentType, String bucket) throws Exception;

    String uploadFile(File file, String bucket, String fileName, String contentType, boolean autoCreateBucket) throws Exception;

    String uploadFile(InputStream is, String bucket, String fileName, String contentType, boolean autoCreateBucket) throws Exception;
}
@Component
public class MinioServiceImpl implements IMinioService {

    private static final Long PART_SIZE = 100 * 1024 * 1024L;

    @Autowired
    private MinioClient minioClient;


    public boolean checkBucketIsExist(String bucket) throws Exception {
        AssertUtils.isEmpty(bucket, "参数不能为空");
        return this.minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
    }

    public boolean createBucket(String bucket) throws Exception {
        AssertUtils.isEmpty(bucket, "参数不能为空");
        if (checkBucketIsExist(bucket)) {
            return true;
        } else {
            this.minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
            return true;
        }
    }

    public String uploadFile(File file, String fileName, String contentType, String bucket) throws Exception {
        AssertUtils.isNull(file, "上传文件不能为空");
        InputStream is = new FileInputStream(file);
        return this.uploadFile(is, fileName, contentType, bucket);
    }

    public String uploadFile(InputStream is, String fileName, String contentType, String bucket) throws Exception {
        return this.uploadFile(is, bucket, fileName, contentType, false);
    }

    public String uploadFile(File file, String bucket, String fileName, String contentType, boolean autoCreateBucket) throws Exception {
        AssertUtils.isNull(file, "上传文件不能为空");
        InputStream is = new FileInputStream(file);
        return this.uploadFile(is, bucket, fileName, contentType, autoCreateBucket);
    }

    public String uploadFile(InputStream is, String bucket, String fileName, String contentType, boolean autoCreateBucket) throws Exception {
        AssertUtils.isNull(is, "上传文件不能为空");
        AssertUtils.isEmpty(bucket, "文件桶不存在");
        AssertUtils.isEmpty(fileName, "文件名不能为空");
        boolean exist = checkBucketIsExist(bucket);
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(bucket)
                .object(fileName)
                .contentType(contentType)
                .stream(is, -1, PART_SIZE)
                .build();
        if (!exist) {
            if (autoCreateBucket) {
                this.createBucket(bucket);
            } else {
                throw new ApplicationException("未获取到有效文件桶");
            }
        }
        this.minioClient.putObject(putObjectArgs);
        return getObjectUrl(bucket, fileName);
    }

    private String getObjectUrl(String bucket, String fileName) throws Exception {
        return this.getObjectUrl(bucket, fileName, null, null);
    }

    /**获取签名
     * 注,有效期时间是7天  据说官方论坛上将,长期有效链接,需要将桶设置成public, 有待研究
     * @param bucket 桶
     * @param fileName 对象名
     * @param value 有效期时间
     * @param timeUnit 时间单位
     * @return
     * @throws Exception
     */
    private String getObjectUrl(String bucket, String fileName, Integer value, TimeUnit timeUnit) throws Exception {
        GetPresignedObjectUrlArgs.Builder builder = GetPresignedObjectUrlArgs.builder();
        builder.bucket(bucket).object(fileName).method(Method.GET);
        if (value != null && value > 0) {
            builder.expiry(value, timeUnit);
        }
        return this.minioClient.getPresignedObjectUrl(builder.build());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值