Compare commits

...

2 Commits

Author SHA1 Message Date
bunny 94d5a3b486 ci: 添加推送脚本 2025-04-07 14:54:10 +08:00
bunny 21403c8f4d fix: 如果minio桶不存在就创建 2025-04-07 14:53:52 +08:00
4 changed files with 73 additions and 5 deletions

5
push.sh Normal file
View File

@ -0,0 +1,5 @@
git checkout master
git merge dev
git push --all
git push --tags
git checkout dev

View File

@ -1,6 +1,9 @@
package cn.bunny.services.utils.minio; package cn.bunny.services.utils.minio;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient; import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import lombok.Data; import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
@ -24,6 +27,30 @@ public class MinioProperties {
@Bean @Bean
public MinioClient minioClient() { public MinioClient minioClient() {
return MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build(); MinioClient minioClient = MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build();
try {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
String publicPolicy = String.format("{\n" +
" \"Version\": \"2012-10-17\",\n" +
" \"Statement\": [\n" +
" {\n" +
" \"Effect\": \"Allow\",\n" +
" \"Principal\": \"*\",\n" +
" \"Action\": [\"s3:GetObject\"],\n" +
" \"Resource\": [\"arn:aws:s3:::%s/*\"]\n" +
" }\n" +
" ]\n" +
"}", bucketName);
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(publicPolicy).build());
}
} catch (Exception exception) {
exception.getStackTrace();
}
return minioClient;
} }
} }

View File

@ -175,4 +175,40 @@ public class MinioUtil {
throw new AuthCustomerException(e.getMessage()); throw new AuthCustomerException(e.getMessage());
} }
} }
/**
* 判断桶是否存在
*
* @param bucketName 桶名称
* @return 布尔值是否存在
*/
public boolean createBucketIfNotExists(String bucketName) {
boolean found = false;
try {
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
// 如果 bucket 不存在就创建
if (!found) makeBucket(bucketName);
return found;
} catch (Exception exception) {
log.error("判断桶是否存在 ------ 失败消息:{}", exception.getLocalizedMessage());
exception.getStackTrace();
}
throw new AuthCustomerException("未初始化 bucket");
}
/**
* 创建桶
*
* @param bucketName 桶的名称
*/
public void makeBucket(String bucketName) {
try {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
} catch (Exception exception) {
exception.getStackTrace();
throw new AuthCustomerException("创建失败");
}
}
} }

View File

@ -19,20 +19,20 @@ logging:
bunny: bunny:
master: master:
host: 192.168.3.137 host: localhost
port: 3306 port: 3306
database: auth_admin database: auth_admin
username: root username: root
password: "123456" password: "123456"
redis: redis:
host: 192.168.3.137 host: localhost
port: 6379 port: 6379
database: 0 database: 0
password: "123456" password: "123456"
minio: minio:
endpointUrl: "http://192.168.3.137:9000" # 连接地址 endpointUrl: "http://localhost:9000" # 连接地址
accessKey: bunny # 用户名 accessKey: bunny # 用户名
secretKey: "12345678" # 登录密码 secretKey: "12345678" # 登录密码
bucket-name: auth-admin # 指定哪个桶 bucket-name: auth-admin # 指定哪个桶