引入依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.10</version>
</dependency>
在类中配置使用
package com.atguigu;
import io.minio.*;
public class TestMinio {
public static void main(String[] args) {
String endpoint = "http://192.168.36.10:9000";
String accesskey = "minioadmin";
String secretkey = "minioadmin";
String bucketName = "hello-minio";
MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accesskey, secretkey).build();
try {
boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if(!bucketExists){
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
String policy = """
{
"Statement" : [ {
"Action" : "s3:GetObject",
"Effect" : "Allow",
"Principal" : "*",
"Resource" : "arn:aws:s3:::%s/*"
} ],
"Version" : "2012-10-17"
}
""".formatted(bucketName);
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
}
minioClient.uploadObject(UploadObjectArgs.builder().filename("E:\\下载\\f2b0e5a3-32d5-41b8-abf8-ef7676b2c35c-transformed-transformed.png").bucket(bucketName).object("维管云阁.jpg").build());
System.out.println("上传成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}