package org.ailun;
import io.minio.*;
import io.minio.messages.*;
import java.util.List;
/**
* @version 1.0
* @since : JDK 11
*/
public class Test {
private static String bucketName = "bucketName";
public static void main(String[] args) {
// http://192.168.100.141:9000/test/2.jpg
// MinioClient client = getMinioClient("http://192.168.100.141:9000", "minio_xxPatG", "minio_eJnEDp");
MinioClient client = getMinioClient("http://192.168.100.147:9000", "admin", "QdJv2iojuW@jtzl");
createBucket(client, bucketName);
setBucketLifecycleRules(client, bucketName, 30);
}
/**
* 设置桶的生命周期规则
*
* @param client 客户端
* @param days 天数
*/
private static void setBucketLifecycleRules(MinioClient client, String bucketName, Integer days) {
AbortIncompleteMultipartUpload abortIncompleteMultipartUpload = null;
Expiration expiration = new Expiration((ResponseDate) null, days, (Boolean) null);
RuleFilter ruleFilter = new RuleFilter("");
String id = null;
NoncurrentVersionExpiration noncurrentVersionExpiration = null;
NoncurrentVersionTransition noncurrentVersionTransition = null;
Transition transition = null;
LifecycleRule lifecycleRule = new LifecycleRule(Status.ENABLED, abortIncompleteMultipartUpload, expiration, ruleFilter, id, noncurrentVersionExpiration, noncurrentVersionTransition, transition);
LifecycleConfiguration lifecycleConfiguration = new LifecycleConfiguration(List.of(lifecycleRule));
SetBucketLifecycleArgs args = SetBucketLifecycleArgs
.builder()
.bucket(bucketName)
.config(lifecycleConfiguration)
.build();
try {
client.setBucketLifecycle(args);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 创建minio的桶
* @param client minio客户端
* @param bucketName 桶名称
*/
private static void createBucket(MinioClient client, String bucketName) {
// 访问权限模板字符串
final String template = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]}," +
"\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::" + bucketName + "\"]}," +
"{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:PutObject\",\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:GetObject\"," +
"\"s3:ListMultipartUploadParts\"],\"Resource\":[\"arn:aws:s3:::" + bucketName + "/*\"]}]}";
try {
boolean exists = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!exists) {
client.makeBucket(MakeBucketArgs.builder().objectLock(false).bucket(bucketName).build());
client.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(template).build());
client.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建minIO客户端
*
* @param minioServerAddr minIO服务接口管理地址 http://192.168.100.141:9000
* @param ak accessKey 也可以是console控制台的登录用户名
* @param sk secretKey 也可以是console控制台的登录密码
* @return
*/
private static MinioClient getMinioClient(String minioServerAddr, String ak, String sk) {
return MinioClient
.builder()
.endpoint(minioServerAddr)
// console 账号
.credentials(ak, sk)
// AKSK账号
// .credentials("bRTaSRALg352qdx1MMGf","q98lFSssKNkOAoxOjn7QXB9ugEsMnJz0688HDHQO")
.build();
}
}
标签:初始化,Minio,s3,及桶,param,client,bucketName,null,String
From: https://www.cnblogs.com/hhddd-1024/p/18430612