各种排查异常后,将之前配置的 secure参数由true改成了false,使用给定的endpoint、port、access key、secret key和一个secure选项(表示的是是否使用https)创建一个Minio client对象。
1.pom依赖
io.minio
minio
3.0.10
2.配置minio
import
import org.springframework.stereotype.Component;
@Component
public class MinIoConfig {
/**
* minio 服务地址 http://ip:port
*/
private String endpoint="http://127.0.0.1:9000";
/**
* 用户名
*/
private String accessKey="your accessKey";
/**
* 密码
*/
private String secretKey="your secretKey ";
/**
* 桶名称
*/
private String bucketName;
private boolean secure=false;
public boolean isSecure() {
return secure;
}
public void setSecure(boolean secure) {
this.secure = secure;
}
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
}
3.工具类
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.xmlpull.v1.XmlPullParserException;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Component
@Configuration
public class MinIoUtils {
@Autowired
private MinIoConfig minIo;
public MinIoUtils(MinIoConfig minIo) {
this.minIo = minIo;
}
private MinioClient instance;
public MinIoUtils() {
}
@PostConstruct
public void init() {
try {
instance = new MinioClient(minIo.getEndpoint(), minIo.getAccessKey(), minIo.getSecretKey(),minIo.isSecure());
} catch (InvalidPortException e) {
e.printStackTrace();
} catch (InvalidEndpointException e) {
e.printStackTrace();
}
}
/**
* 判断 bucket是否存在
*
* @param bucketName
* @return
*/
public boolean bucketExists(String bucketName) {
try {
return instance.bucketExists(bucketName);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 创建 bucket
*
* @param bucketName
*/
public void makeBucket(String bucketName) {
try {
boolean isExist = instance.bucketExists(bucketName);
if (!isExist) {
instance.makeBucket(bucketName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param filename
*/
public void putObject(String bucketName, String objectName, String filename) {
try {
instance.putObject(bucketName, objectName, filename,null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param stream
*/
public void putObject(String bucketName, String objectName, InputStream stream) {
try {
instance.putObject(bucketName, objectName, stream, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件
*
* @param bucketName
* @param objectName
*/
public void removeObject(String bucketName, String objectName) {
try {
instance.removeObject(bucketName, objectName);
} catch (Exception e) {
e.printStackTrace();
}
}
public void putObject(String bucketName, String fileName, InputStream inputStream, String contentType) {
try {
instance.putObject(bucketName, fileName, inputStream, contentType);
} catch (InvalidBucketNameException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InsufficientDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoResponseException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (InternalException e) {
e.printStackTrace();
} catch (InvalidArgumentException e) {
e.printStackTrace();
}
}
}
4.controller层
@ApiOperation(value = "使用minio文件上传")
@PostMapping(value = "/uploadFile")
public Result uploadFile(@RequestParam("file")MultipartFile file,@RequestParam @ApiParam(name = "bucketName", value = "对象存储桶名称") String bucketName) {
try {
String filename = uploadFileService.uploadFile(file, bucketName);
return Result.success(filename);
} catch (Exception e) {
e.printStackTrace();
return Result.fail("上传失败");
}
}
5.实现类
@Override
public String uploadFile(MultipartFile file, String bucketName) throws Exception {
String s = null;
try {
String filename = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 文件存储的目录结构
String objectName = sdf.format(new Date()) + ":" + filename;
// 存储文件
InputStream is = file.getInputStream();
minIoUtils.putObject(bucketName, filename, file.getInputStream(), file.getContentType());
log.info("文件上传成功!");
s = minIoConfig.getEndpoint() + "/" + bucketName + "/" + objectName;
return s;
} catch (Exception e) {
log.info("上传发生错误: {}!", e.getMessage());
throw new MyException("上传失败");
}
}