You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
6.5 KiB
191 lines
6.5 KiB
package {{ package.Common }}.unit;
|
|
import {{ package.Common }}.config.MinioConfig;
|
|
|
|
import io.minio.*;
|
|
import io.minio.http.Method;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/****
|
|
* @Description: 上传 文件组件
|
|
* @Author: {{author}}
|
|
* @Date: {{date}}
|
|
* @Wechat: {{ wechat }}
|
|
*/
|
|
@Slf4j
|
|
@Component
|
|
public class MinioUpComponent {
|
|
|
|
private final static String separator = "/";
|
|
@Autowired
|
|
private MinioConfig minioConfig;
|
|
@Autowired
|
|
private MinioClient minioClient;
|
|
|
|
/**
|
|
* @param dirPath
|
|
* @param filename yyyy/mm/dd/file.jpg
|
|
* @return
|
|
*/
|
|
public static String builderFilePath(String dirPath, String filename) {
|
|
StringBuilder stringBuilder = new StringBuilder(50);
|
|
if (!StringUtils.isEmpty(dirPath)) {
|
|
stringBuilder.append(dirPath).append(separator);
|
|
}
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
String todayStr = sdf.format(new Date());
|
|
stringBuilder.append(todayStr).append(separator);
|
|
stringBuilder.append(filename);
|
|
return stringBuilder.toString();
|
|
}
|
|
|
|
/**
|
|
* 上传图片文件
|
|
*
|
|
* @param prefix 文件前缀
|
|
* @param filename 文件名
|
|
* @param inputStream 文件流
|
|
* @return 文件全路径
|
|
*/
|
|
public String uploadImg(String prefix, String filename, InputStream inputStream) {
|
|
String filePath = builderFilePath(prefix, filename);
|
|
try {
|
|
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
|
.object(filePath)
|
|
.contentType("image/jpg")
|
|
.bucket(minioConfig.getBucketName()).stream(inputStream, inputStream.available(), -1)
|
|
.build();
|
|
minioClient.putObject(putObjectArgs);
|
|
StringBuilder urlPath = new StringBuilder(minioConfig.getReadPath());
|
|
urlPath.append(separator + minioConfig.getBucketName());
|
|
urlPath.append(separator);
|
|
urlPath.append(filePath);
|
|
return urlPath.toString();
|
|
} catch (Exception ex) {
|
|
log.error("minio put file error.", ex);
|
|
throw new RuntimeException("上传文件失败");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传html文件
|
|
*
|
|
* @param prefix 文件前缀
|
|
* @param filename 文件名
|
|
* @param inputStream 文件流
|
|
* @return 文件全路径
|
|
*/
|
|
public String uploadHtml(String prefix, String filename, InputStream inputStream) {
|
|
String filePath = builderFilePath(prefix, filename);
|
|
try {
|
|
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
|
.object(filePath)
|
|
.contentType("text/html")
|
|
.bucket(minioConfig.getBucketName())
|
|
.stream(inputStream, inputStream.available(), -1)
|
|
.build();
|
|
minioClient.putObject(putObjectArgs);
|
|
StringBuilder urlPath = new StringBuilder(minioConfig.getReadPath());
|
|
urlPath.append(separator + minioConfig.getBucketName());
|
|
urlPath.append(separator);
|
|
urlPath.append(filePath);
|
|
return urlPath.toString();
|
|
} catch (Exception ex) {
|
|
log.error("minio put file error.", ex);
|
|
ex.printStackTrace();
|
|
throw new RuntimeException("上传文件失败");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 防盗链接有效期按分钟
|
|
*
|
|
* @param filePath 文件前缀
|
|
* @param expiry 超时分钟
|
|
* @return 文件全路径
|
|
*/
|
|
public String getPresignObjectUrl(String filePath, Integer expiry) {
|
|
try {
|
|
filePath = filePath.replace("http://health.reglory.com.cn:9001/health-bucket/", "");
|
|
GetPresignedObjectUrlArgs putObjectArgs = GetPresignedObjectUrlArgs.builder()
|
|
.object(filePath)
|
|
.bucket(minioConfig.getBucketName())
|
|
.method(Method.GET)
|
|
// .region("health.reglory.com.cn:9001")
|
|
.expiry(expiry, TimeUnit.MINUTES)
|
|
.build();
|
|
return minioClient.getPresignedObjectUrl(putObjectArgs);
|
|
} catch (Exception ex) {
|
|
log.error("minio put file error.", ex);
|
|
ex.printStackTrace();
|
|
throw new RuntimeException("上传文件失败");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除文件
|
|
*
|
|
* @param pathUrl 文件全路径
|
|
*/
|
|
public void delete(String pathUrl) {
|
|
String key = pathUrl.replace(minioConfig.getEndpoint() + "/", "");
|
|
int index = key.indexOf(separator);
|
|
String bucket = key.substring(0, index);
|
|
String filePath = key.substring(index + 1);
|
|
// 删除Objects
|
|
RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(filePath).build();
|
|
try {
|
|
minioClient.removeObject(removeObjectArgs);
|
|
} catch (Exception e) {
|
|
log.error("minio remove file error. pathUrl:{}", pathUrl);
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 下载文件
|
|
*
|
|
* @param pathUrl 文件全路径
|
|
* @return 文件流
|
|
*/
|
|
public byte[] downLoadFile(String pathUrl) {
|
|
String key = pathUrl.replace(minioConfig.getEndpoint() + "/", "");
|
|
int index = key.indexOf(separator);
|
|
//String bucket = key.substring(0, index);
|
|
String filePath = key.substring(index + 1);
|
|
InputStream inputStream = null;
|
|
try {
|
|
inputStream = minioClient.getObject(
|
|
GetObjectArgs.builder().bucket(minioConfig.getBucketName()).object(filePath).build()
|
|
);
|
|
} catch (Exception e) {
|
|
log.error("minio down file error. pathUrl:{}", pathUrl);
|
|
e.printStackTrace();
|
|
}
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
byte[] buff = new byte[100];
|
|
int rc = 0;
|
|
while (true) {
|
|
try {
|
|
if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
byteArrayOutputStream.write(buff, 0, rc);
|
|
}
|
|
return byteArrayOutputStream.toByteArray();
|
|
}
|
|
}
|
|
|
|
|