学习笔记-minio

14 篇文章 0 订阅
1 篇文章 0 订阅

minio

  • 分布式文件存储系统
  • 概念
    • Bucket
    • Object
      • 存储文件
    • Drive
      • 磁盘
    • Set
      • Drive集合
  • 纠删码(EC)
    • Erasure Code
    • 保证高可靠性
  • 安装
    • docker
    • docker compose
  • 部署
    • 纠删码模式部署
    • 分布式集群部署
  • 客户端mc操作
    • mc config host ls
      • 查看minio服务器名称与地址
    • mc config host add xxx http://xxx.xx.xx.xx:port username password
      • 添加minio服务器到列表
    • mc config host remove xxx
      • 删除列表中对应的minio服务器
    • mc ls 服务器名称
      • 查看minio服务文件列表
    • mc cp minio服务器名/文件路径/文件名 D:\xx\xx
      • 下载文件
    • mc cp D:\xx\xx minio服务器名/桶
      • 上传文件
    • mc rm minio服务器名/文件路径/文件名
      • 删除文件
    • mc mb minio服务器名/桶名
      • 创建bucket
    • mc rm --force minio服务器名/桶名
      • 强制删除bucket(bucket不为空需要强制删除)
    • mc du minio服务器(/路径)
      • 磁盘使用情况
    • mc admin user list minio服务器
      • 查看用户信息
    • mc admin user add minio服务器 newUser
      • 添加新用户
    • mc policy list 服务器
      • 查看策略列表
    • mc admin policy info 服务器 策略名
      • 查看策略详情
    • mc admin policy add 服务器 策略名 策略文件地址
      • 添加策略
    • mc admin policy set 用户 策略名
      • 为用户设置策略

java整合

  • 依赖
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.3.0</version>
</dependency>
<dependency>
    <groupId>me.tongfei</groupId>
    <artifactId>progressbar</artifactId>
    <version>0.5.3</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.8.1</version>
</dependency>
 // 连接minio
 MinioClient minioClient =
     MinioClient.builder()
         .endpoint("https://play.min.io")
         .credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
         .build();
// 创建bucket
String bucketName = "xxx";
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if(!found){
    // 不存在
    minioClient.makeBucket(MakeBuketArgs.builder().bucket(bucketName).build());
}
// 上传文件
minioClient.uploadObject(
    UploadObjectArgs.builder()
        .bucket(bucketName)
        .object("xxx")      // 上传文件路径
        .filename("xxx")    // 本地磁盘路径
        .build());

// 下载
minioClient.downLoadObject(
    DownLoadObjectArgs.builder()
        .bucket(bucketName)
        .object("xxx")      // 上传文件路径
        .filename("xxx")    // 本地磁盘路径
        .build());

springboot整合

minio:
  endpoint:xxx
  accesskey:xxx
  secretKey:xxx
  bucketName:xxx
@Data
@Component
@ConfigurationProperties(prefix="minio")
public class MinioProperties{
    private String endpoint;
    private String accesskey;
    private String secretkey;
    
}
@Configuration
public class MinioConfig{
    @Autowired
    private MinioProperties minioProperties;
    
    @Bean
    public MinioClient minioClient(){
         MinioClient minioClient = 
            MinioClient.builder()
                .endpoint(minioProperties.getEndPoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        return minioClient;
    }
    
}

@Component
public class MinioUtil {

	@Autowired
	private MinioClient minioClient;

	/**
	 * 文件上传
	 */
	public Boolean upload(MultipartFile file, String filePath, String tempFileName, String bucketName) {
		try {
			if (!bucketExists(bucketName)) {
				makeBucket(bucketName);
			}
			PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName)
				.object(filePath + tempFileName)
				.stream(file.getInputStream(), file.getSize(), -1)
				.contentType(file.getContentType()).build();
			//文件名称相同会覆盖
			ObjectWriteResponse response = minioClient.putObject(objectArgs);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 预览文件
	 */
	public void preview(String bucketName, String fileUrl, String openStyle, String fileName, HttpServletResponse res) {
		GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
			.object(fileUrl + fileName).build();
		try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
			byte[] buf = new byte[1024];
			int len;
			try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
				while ((len = response.read(buf)) != -1) {
					os.write(buf, 0, len);
				}
				os.flush();
				String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
				byte[] bytes = os.toByteArray();
				res.setCharacterEncoding("utf-8");
				String src = "application/" + suffix;
				res.setContentType(src);
				res.addHeader("Content-Disposition", openStyle + ";fileName=" + fileName);
				try (ServletOutputStream stream = res.getOutputStream()) {
					stream.write(bytes);
					stream.flush();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查看存储bucket是否存在
	 */
	public Boolean bucketExists(String bucketName) {
		Boolean found;
		try {
			found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return found;
	}

	/**
	 * 创建存储bucket
	 */
	public Boolean makeBucket(String bucketName) {
		try {
			minioClient.makeBucket(MakeBucketArgs.builder()
				.bucket(bucketName)
				.build());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 删除存储bucket
	 */
	public Boolean removeBucket(String bucketName) {
		try {
			minioClient.removeBucket(RemoveBucketArgs.builder()
				.bucket(bucketName)
				.build());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}


	/**
	 * 文件下载
	 */
	public void download(String bucketName, String fileUrl, String fileName, HttpServletResponse res) {
		GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
			.object(fileUrl + fileName).build();
		try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
			byte[] buf = new byte[1024];
			int len;
			try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
				while ((len = response.read(buf)) != -1) {
					os.write(buf, 0, len);
				}
				os.flush();
				byte[] bytes = os.toByteArray();
				res.setCharacterEncoding("utf-8");
				//设置强制下载不打开
				res.setContentType("application/force-download");
				res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
				try (ServletOutputStream stream = res.getOutputStream()) {
					stream.write(bytes);
					stream.flush();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 批量删除文件对象
	 */
	public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
		List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
		Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
		return results;
	}

	//文件删除
	@DeleteMapping
	public boolean delete(String objectName, String bucketName) {
		try {
			RemoveObjectArgs objectArgs = RemoveObjectArgs.builder().object(objectName).bucket(bucketName).build();
			minioClient.removeObject(objectArgs);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 上传文件
	 */
	public boolean putObject(String bucketName, String objectName, InputStream stream) throws Exception {
		try {
			PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName)
				.object(objectName).contentType("application/octet-stream").stream(stream, stream.available(), -1).build();
			minioClient.putObject(objectArgs);
			return true;
		} catch (Exception e) {
			return false;
		}


	}


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值