MINIO自动创建bucket【Java】

刚整合完x-file-storage,发现之前自己写了一个自动创建minio bucket的代码。放到博客文章,以免以后找不到了。

package org.ccframe.subsys.core.service;

import io.minio.*;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

@Service
@Deprecated //"直接使用公共的FileService"
public class MinioService {

    private static final String POLICY_PATTERN = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::%s/*\"]}]}"; 
    
    private static final String LOCK_CREATE_BUCKET_LOCK = "createBucketLock";
    
    @Autowired
    @Lazy
    private MinioClient minioClient;
    
    @Autowired
    private RedissonClient redissonClient;
    
    private RLock createBucketLock;
    
    @Value("${app.minio.enabled:false}")
    public boolean enabled;

    private void checkBucket(String bucket) throws Exception{
       if(! minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
          createBucketLock = redissonClient.getLock(LOCK_CREATE_BUCKET_LOCK);
          createBucketLock.lock(5, TimeUnit.SECONDS);
          try {
             if(! minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) { // 可能阻塞后其他线程已经建立好bucket了
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
                for(int i = 0; i < 6; i ++) {
                   TimeUnit.MICROSECONDS.sleep(500);
                   if(minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {  //等待建立完毕,才能去设置权限
                      minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucket).config(
                         String.format(POLICY_PATTERN, bucket)
                      ).build());
                      break;
                   }
                }
             }
          } finally {
             createBucketLock.unlock();
          }
       }
    }

    public void putObject(String path, InputStream stream) {
       String bucket = path.split("\\\\|\\/")[0];
       try {
          checkBucket(bucket);
          minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(path.substring(bucket.length() + 1)).stream(stream, stream.available(), -1).build());
       } catch (Exception e) {
          throw new RuntimeException(e);
       }
    }

    public InputStream getObject(String path) {
       String bucket = path.split("\\\\|\\/")[0];
       try {
          return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(path.substring(bucket.length() + 1)).build());
       } catch (Exception e) {
          throw new RuntimeException(e);
       }
    }

    public void removeObject(String path) {
       String bucket = path.split("\\\\|\\/")[0];
       try {
          minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(path.substring(bucket.length() + 1)).build());
       } catch (Exception e) {
          throw new RuntimeException(e);
       }
    }

    public StatObjectResponse statObject(String path) {
       String bucket = path.split("\\\\|\\/")[0];
       try {
          return minioClient.statObject(StatObjectArgs.builder().bucket(bucket).object(path.substring(bucket.length() + 1)).build());
       } catch (Exception e) {
          throw new RuntimeException(e);
       }
       
    }
    
    public void copyObject(String sourcePath, String targetPath) {
       String sourceBucket = sourcePath.split("\\\\|\\/")[0];
       String targetBucket = targetPath.split("\\\\|\\/")[0];
       try {
          CopySource copySource = CopySource.builder().bucket(sourceBucket).object(sourcePath.substring(sourceBucket.length() + 1)).build();
          minioClient.copyObject(CopyObjectArgs.builder().source(copySource).bucket(targetBucket).object(targetPath.substring(targetBucket.length() + 1)).build());
       } catch (Exception e) {
          throw new RuntimeException(e);
       }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FoxMale007

文章非V全文可读,觉得好请打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值