刚整合完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);
}
}
}