minio对接
This commit is contained in:
@@ -180,6 +180,12 @@
|
||||
<version>3.3.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>8.5.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package minio;
|
||||
|
||||
import com.aliyun.oss.ServiceException;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class MinIoUploadService {
|
||||
@Autowired
|
||||
private MinioClient minioClient;
|
||||
private static String bucket="ngwl";
|
||||
private static String host="http://10.102.192.5:6001/";
|
||||
private static String accessKey="minioadmin";
|
||||
private static String secretKey="gmminioadmin";
|
||||
/**
|
||||
* minio 工具客户端
|
||||
*/
|
||||
private static MinioClient minioClient1 = null;
|
||||
|
||||
private static String FILE_URL;
|
||||
|
||||
/**
|
||||
* 上传到minio
|
||||
* @param fileName
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public boolean uploadFileToMinIo(MultipartFile multipartFile, String fileName) {
|
||||
try {
|
||||
initMinio();
|
||||
InputStream inputStream = multipartFile.getInputStream();
|
||||
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
||||
.object(fileName)//文件名
|
||||
.bucket(bucket)//桶名词 与minio创建的名词一致
|
||||
// fileInputStream.available() 表示一直有内容就会上传 -1 表示将所有的相关文件的内容都上传
|
||||
.stream(inputStream, inputStream.available(), -1) //文件流
|
||||
.build();
|
||||
minioClient.putObject(putObjectArgs);
|
||||
return true;
|
||||
}catch (Exception e){
|
||||
//log.error(e.getMessage(),e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private MinioClient initMinio() {
|
||||
try{
|
||||
MinioClient minioClient = MinioClient.builder()
|
||||
.endpoint(host)
|
||||
.credentials(accessKey, secretKey)
|
||||
.build();
|
||||
return minioClient;
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return minioClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* 文件上传成功,返回文件完整访问路径
|
||||
* 文件上传失败,返回 null
|
||||
*
|
||||
* @param multipartFile 待上传文件
|
||||
* @param fileDir 文件保存目录
|
||||
* @return oss 中的相对文件路径
|
||||
*/
|
||||
/* public static String upload(MultipartFile multipartFile, String fileDir) {
|
||||
|
||||
|
||||
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||
//初始化minio
|
||||
initMinio();
|
||||
// bucket 不存在,创建
|
||||
if (!minioClient1.bucketExists(bucket)) {
|
||||
minioClient1.makeBucket(bucket);
|
||||
}
|
||||
// 上传文件的名称
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
|
||||
// PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
|
||||
// // 文件的ContentType
|
||||
// putObjectOptions.setContentType(multipartFile.getContentType());
|
||||
|
||||
SimpleDateFormat sdYear = new SimpleDateFormat("yyyy");
|
||||
SimpleDateFormat sdMonth = new SimpleDateFormat("MM");
|
||||
SimpleDateFormat sdDay = new SimpleDateFormat("dd");
|
||||
String year = sdYear.format(new Date());
|
||||
String month = sdMonth.format(new Date());
|
||||
String day = sdDay.format(new Date());
|
||||
String filrDir = "wlhy/" + year + "/" + month + "/" + day + "/";
|
||||
|
||||
// minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, putObjectOptions);
|
||||
minioClient1.putObject(bucket, filrDir+timestamp+fileName, inputStream, multipartFile.getContentType());
|
||||
// 返回访问路径
|
||||
String fileurl = host+ "/"+bucket + "/"+filrDir+timestamp+ UriUtils.encode(fileName, StandardCharsets.UTF_8);
|
||||
System.out.println("fileurl===="+fileurl);
|
||||
FILE_URL = fileurl;
|
||||
return fileurl;
|
||||
} catch (ServiceException e){
|
||||
log.error(e.getErrorMessage());
|
||||
} catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
return FILE_URL;
|
||||
}*/
|
||||
}
|
||||
Reference in New Issue
Block a user