springCloud 整合minio
1.引入pom
<minio.version>8.0.3</minio.version>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
</dependency>
2.yml
biz:
oss:
# resources-url是带有bucket的
resources-url: http://192.168.0.20:9000/mall4cloud
# 文件上传类型 0.阿里云 1.minio
type: 1
endpoint: http://192.168.0.20:9000
bucket: mall4cloud
access-key-id: admin
access-key-secret: admin123456
3.实体类生成
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author FrozenWatermelon
* @date 2020/9/10
*/
@RefreshScope
@Configuration
public class OssConfig {
@Value("${biz.oss.endpoint}")
private String endpoint;
@Value("${biz.oss.bucket}")
private String bucket;
@Value("${biz.oss.access-key-id}")
private String accessKeyId;
@Value("${biz.oss.access-key-secret}")
private String accessKeySecret;
@Value("${biz.oss.type}")
private Integer ossType;
/**
* 最大上传长度单位m,默认20M
*/
@Value("${biz.oss.maxLength:20}")
private Integer maxLength;
public String getAccessId() {
return accessKeyId;
}
public String getBucket() {
return bucket;
}
public String getEndpoint() {
return endpoint;
}
public Integer getMaxLength() {
return maxLength;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
public void setMaxLength(Integer maxLength) {
this.maxLength = maxLength;
}
public Integer getOssType() {
return ossType;
}
public void setOssType(Integer ossType) {
this.ossType = ossType;
}
}
4.封装工具类
import com.mall4j.cloud.common.exception.Mall4cloudException;
import com.mall4j.cloud.common.response.ResponseEnum;
import io.minio.*;
import io.minio.http.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* @author FrozenWatermelon
*/
@Component
public class MinioTemplate implements InitializingBean {
@Autowired
private OssConfig ossConfig;
private MinioClient minioClient;
static final Logger logger = LoggerFactory.getLogger(MinioTemplate.class);
@Override
public void afterPropertiesSet() throws Exception {
this.minioClient = MinioClient.builder().endpoint(ossConfig.getEndpoint())
.credentials(ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret())
.build();
}
/**
* 删除文件
*
* @param objectName 文件名称
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
*/
public void removeObject(String objectName) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder().object(objectName).bucket(ossConfig.getBucket()).build());
}
/**
* 获得上传的URL
* @param objectName
*/
public String getPresignedObjectUrl(String objectName){
try {
String presignedObjectUrl = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(ossConfig.getBucket()).object(objectName).expiry(10, TimeUnit.MINUTES).method(Method.PUT).build());
return presignedObjectUrl;
} catch (Exception e) {
e.printStackTrace();
throw new Mall4cloudException(ResponseEnum.EXCEPTION);
}
}
public void uploadMinio(byte[] bytes, String filePath, String contentType) throws IOException {
InputStream input = null;
try {
input = new ByteArrayInputStream(bytes);
minioClient.putObject(
PutObjectArgs.builder()
.bucket(ossConfig.getBucket())
.contentType(contentType)
.stream(input, input.available(), -1)
.object(filePath)
.build()
);
} catch (Exception e) {
logger.error("minio上传文件错误:", e);
} finally {
if (Objects.nonNull(input)) {
input.close();
}
}
}
}
posted on 2023-01-04 17:22 AnkangWenqiang 阅读(722) 评论(0) 收藏 举报
浙公网安备 33010602011771号