java使用MinIo管理文件

配置文件application.yml

#minio配置
minio:
  endpoint: http://ip:port
  access-key: MinIo配置的用户名
  secret-key: MinIo配置的密码
  bucketname: xxx桶名

import io.minio.BucketExistsArgs;
import io.minio.ListObjectsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.SetBucketPolicyArgs;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description: 文件上传下载工具类(minio)
 */
@Component
public class FileUpDownLoadUtil {

    @Value("${minio.endpoint}")
    private String endpoint;

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

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

    @Value("${minio.bucketname}")
    private String bucketName;

    /**
     * 上传文件
     * @param file 上传的文件
     * @param prefix 前缀
     * @param type 文件类型
     * @return 返回文件存储路径
     * @throws Exception
     */
    public String uploadFile(MultipartFile file, String prefix, String type) throws Exception {
        MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
        String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
                + "\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:ListBucketMultipartUploads\","
                + "\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::"
                + bucketName + "\"]},"
                + "{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},"
                + "\"Action\":[\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:GetObject\","
                + "\"s3:ListMultipartUploadParts\",\"s3:PutObject\"],"
                + "\"Resource\":[\"arn:aws:s3:::" + bucketName + "/*\"]}]}";
        minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().config(policy).bucket(bucketName).build());
        String filePath = "";
        if ("csv".equals(type)) {
            filePath = prefix + "/" + UuidUtil.getUuid()
                    + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        } else {
            filePath = prefix + "/" + UuidUtil.getUuid();
        }
        minioClient.putObject(PutObjectArgs.builder()
                .stream(file.getInputStream(), file.getSize(), PutObjectArgs.MIN_MULTIPART_SIZE)
                .object(filePath)
                .contentType(file.getContentType())
                .bucket(bucketName)
                .build());
        return "/" + bucketName + "/" + filePath;
    }

    /**
     * 删除文件
     * @param path 文件路径
     * @throws Exception
     */
    public void delete(String path) throws Exception {
        MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
        minioClient.removeObject(RemoveObjectArgs.builder()
                .bucket(bucketName)
                .object(path)
                .build());
    }

    /**
     * 删除文件夹
     * @param path
     */
    public void deleteFolder(String path) {
        MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
        if (path.endsWith("/")) {
            Iterable<Result<Item>> resultsList = minioClient.listObjects(ListObjectsArgs.builder()
                    .bucket(bucketName)
                    .prefix(path)
                    .build()
            );
            resultsList.forEach(e -> {
                try {
                    String childPath = e.get().objectName();
                    if (childPath.endsWith("/")) {
                        deleteFolder(childPath);
                    } else {
                        minioClient.removeObject(RemoveObjectArgs.builder()
                                .bucket(bucketName)
                                .object(childPath)
                                .build());
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        }
    }





}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值