依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
对应版本

配置工具类
package com.github.devcude.comp.plazamall.coreservice.configuration.core;
import com.github.devcude.comp.plazamall.coreservice.util.core.DateFormUtils;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.Result;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.MinioException;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
@Slf4j
public class MinioConfig implements InitializingBean {
@Value(value = "${minio.bucket}")
private String bucket;
@Value(value = "${minio.host}")
private String host;
@Value(value = "${minio.accessKey}")
private String accessKey;
@Value(value = "${minio.secretKey}")
private String secretKey;
private MinioClient minioClient;
@Override
public void afterPropertiesSet() throws Exception {
Assert.hasText(accessKey, "Minio accessKey为空");
Assert.hasText(secretKey, "Minio secretKey为空");
this.minioClient = new MinioClient(this.host, this.accessKey, this.secretKey);
}
public void createBucketWithPublicAccess(String bucket) {
try {
if (!minioClient.bucketExists(bucket)) {
minioClient.makeBucket(bucket);
System.out.println("Bucket created: " + bucket);
} else {
System.out.println("Bucket already exists: " + bucket);
}
String policyJson = "{\n" +
" \"Version\": \"2012-10-17\",\n" +
" \"Statement\": [\n" +
" {\n" +
" \"Sid\": \"PublicReadGetObject\",\n" +
" \"Effect\": \"Allow\",\n" +
" \"Principal\": \"*\",\n" +
" \"Action\": \"s3:GetObject\",\n" +
" \"Resource\": \"arn:aws:s3:::" + bucket + "/*\"\n" +
" }\n" +
" ]\n" +
"}";
SetBucketPolicyArgs policyArgs = SetBucketPolicyArgs.builder()
.bucket(bucket)
.config(policyJson)
.build();
minioClient.setBucketPolicy(policyArgs);
log.info("桶 {} 设置为 public-read 权限!", bucket);
System.out.println("Bucket access policy set to public-read");
} catch (MinioException e) {
System.err.println("Minio exception: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public String putObject(MultipartFile file) throws Exception {
try {
System.out.println("host=" + host);
System.out.println("accessKey=" + accessKey);
System.out.println("secretKey=" + secretKey);
String bucket = findAvailableBucket();
byte[] byteArr = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);
String filename = file.getOriginalFilename();
String prefix = filename.substring(filename.lastIndexOf(".") + 1);
Calendar calendar = Calendar.getInstance();
Date time = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String buckfilename = format.format(time);
String uuid = UUID.randomUUID().toString().replace("-", "");
String fileName = buckfilename + "/" + uuid + "." + prefix;
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucket).object(fileName)
.stream(inputStream, file.getSize(), -1).contentType(file.getContentType()).build();
minioClient.putObject(objectArgs);
boolean flag = bucketExists(bucket);
String url = "";
if (flag) {
url = minioClient.getObjectUrl(bucket, fileName);
}
log.info("文件url:{}", url);
return url;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
public List<String> listBucketNames()
throws Exception {
List<Bucket> bucketList = listBuckets();
List<String> bucketListName = new ArrayList<>();
for (Bucket bucket : bucketList) {
bucketListName.add(bucket.name());
}
return bucketListName;
}
private String findAvailableBucket() throws Exception {
List<String> bucketNames = listBucketNames();
String resBucketName = null;
for (String bucketName : bucketNames) {
long fileCount = getBucketFileCount(bucketName);
log.info("桶:" + bucketName + "-" + "文件数:" + fileCount);
if (fileCount < 48000) {
resBucketName = bucketName;
break;
}
}
if (StringUtils.isEmpty(resBucketName)) {
String newBucketName = "renrenxueoss-" + DateFormUtils.dateFormat(new Date(), "yyyyMMddHHmmss");
createBucketWithPublicAccess(newBucketName);
log.info("上个桶文件数已满,创建新桶:{}",newBucketName);
return newBucketName;
}
return resBucketName;
}
private long getBucketFileCount(String bucketName) throws Exception {
long fileCount = 0;
if (bucketExists(bucketName)) {
Iterable<Result<Item>> objects = listObjects(bucketName);
for (Result<Item> result : objects) {
fileCount++;
}
}
return fileCount;
}
public List<Bucket> listBuckets()
throws Exception {
return minioClient.listBuckets();
}
public boolean bucketExists(String bucketName) throws Exception {
boolean flag = minioClient.bucketExists(bucketName);
if (flag) {
return true;
}
return false;
}
public boolean makeBucket(String bucketName)
throws Exception {
boolean flag = bucketExists(bucketName);
if (!flag) {
minioClient.makeBucket(bucketName);
return true;
} else {
return false;
}
}
public boolean removeBucket(String bucketName)
throws Exception {
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<Item>> myObjects = listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
if (item.size() > 0) {
return false;
}
}
minioClient.removeBucket(bucketName);
flag = bucketExists(bucketName);
if (!flag) {
return true;
}
}
return false;
}
public Iterable<Result<Item>> listObjects(String bucketName) throws Exception {
boolean flag = bucketExists(bucketName);
if (flag) {
return minioClient.listObjects(bucketName);
}
return null;
}
public List<String> listObjectNames(String bucketName) throws Exception {
List<String> listObjectNames = new ArrayList<>();
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<Item>> myObjects = listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
listObjectNames.add(item.objectName());
}
}
return listObjectNames;
}
public boolean removeObject(String bucketName, String objectName) throws Exception {
boolean flag = bucketExists(bucketName);
if (flag) {
List<String> objectList = listObjectNames(bucketName);
for (String s : objectList) {
if (s.equals(objectName)) {
minioClient.removeObject(bucketName, objectName);
return true;
}
}
}
return false;
}
public String getObjectUrl(String bucketName, String objectName) throws Exception {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
url = minioClient.getObjectUrl(bucketName, objectName);
}
return url;
}
}
配置
#minio配置
minio.user=minio_Jk****
minio.pwd=minio_fZ****
minio.accessKey=xRJAmMi5azFDWp63****
minio.secretKey=D0iyW73vOigXeay4sOVXuxrueb90ITpC8JAh****
minio.bucket=plaza
minio.host=https://ylioss.****.com