|
|
@ -1,26 +1,13 @@
|
|
|
|
package com.gccloud.dataroom.core.utils;
|
|
|
|
package com.gccloud.dataroom.core.utils;
|
|
|
|
|
|
|
|
|
|
|
|
import com.gccloud.dataroom.core.config.bean.DataRoomMinioConfig;
|
|
|
|
import com.gccloud.dataroom.core.config.bean.DataRoomMinioConfig;
|
|
|
|
import io.minio.BucketExistsArgs;
|
|
|
|
import io.minio.*;
|
|
|
|
import io.minio.GetObjectArgs;
|
|
|
|
|
|
|
|
import io.minio.GetPresignedObjectUrlArgs;
|
|
|
|
|
|
|
|
import io.minio.MakeBucketArgs;
|
|
|
|
|
|
|
|
import io.minio.MinioClient;
|
|
|
|
|
|
|
|
import io.minio.RemoveObjectArgs;
|
|
|
|
|
|
|
|
import io.minio.errors.ErrorResponseException;
|
|
|
|
|
|
|
|
import io.minio.errors.InsufficientDataException;
|
|
|
|
|
|
|
|
import io.minio.errors.InternalException;
|
|
|
|
|
|
|
|
import io.minio.errors.InvalidResponseException;
|
|
|
|
|
|
|
|
import io.minio.errors.ServerException;
|
|
|
|
|
|
|
|
import io.minio.errors.XmlParserException;
|
|
|
|
|
|
|
|
import io.minio.http.Method;
|
|
|
|
import io.minio.http.Method;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Minio快捷操作工具类
|
|
|
|
* Minio快捷操作工具类
|
|
|
@ -28,11 +15,12 @@ import java.security.NoSuchAlgorithmException;
|
|
|
|
* @author Acechengui
|
|
|
|
* @author Acechengui
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Component
|
|
|
|
@Component
|
|
|
|
|
|
|
|
@ConditionalOnProperty(prefix = "gc.starter.file", name = "type", havingValue = "minio")
|
|
|
|
public class MinioFileInterface {
|
|
|
|
public class MinioFileInterface {
|
|
|
|
@Autowired
|
|
|
|
@Resource
|
|
|
|
private DataRoomMinioConfig minioConfig;
|
|
|
|
private DataRoomMinioConfig minioConfig;
|
|
|
|
|
|
|
|
|
|
|
|
private MinioClient init() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
|
private MinioClient init() throws Exception {
|
|
|
|
MinioClient build = MinioClient.builder().endpoint(minioConfig.getUrl()).credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey()).build();
|
|
|
|
MinioClient build = MinioClient.builder().endpoint(minioConfig.getUrl()).credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey()).build();
|
|
|
|
//判断bucket是否存在,没有就创建
|
|
|
|
//判断bucket是否存在,没有就创建
|
|
|
|
boolean found =build.bucketExists(BucketExistsArgs.builder().bucket(minioConfig.getBucketName()).build());
|
|
|
|
boolean found =build.bucketExists(BucketExistsArgs.builder().bucket(minioConfig.getBucketName()).build());
|
|
|
@ -46,7 +34,7 @@ public class MinioFileInterface {
|
|
|
|
* 删除文件
|
|
|
|
* 删除文件
|
|
|
|
* @param objectName 文件名(路径)
|
|
|
|
* @param objectName 文件名(路径)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void deleteObject(String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
|
public void deleteObject(String objectName) throws Exception {
|
|
|
|
MinioClient minioClient = init();
|
|
|
|
MinioClient minioClient = init();
|
|
|
|
//删除文件时,如果对应文件夹下的文件已删除完,文件夹会自动删除
|
|
|
|
//删除文件时,如果对应文件夹下的文件已删除完,文件夹会自动删除
|
|
|
|
minioClient.removeObject(RemoveObjectArgs.builder()
|
|
|
|
minioClient.removeObject(RemoveObjectArgs.builder()
|
|
|
@ -60,7 +48,7 @@ public class MinioFileInterface {
|
|
|
|
* 下载文件
|
|
|
|
* 下载文件
|
|
|
|
* @param objectName 文件名(路径)
|
|
|
|
* @param objectName 文件名(路径)
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public InputStream download(String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
|
public InputStream download(String objectName) throws Exception {
|
|
|
|
MinioClient minioClient = init();
|
|
|
|
MinioClient minioClient = init();
|
|
|
|
// 获取文件输入流
|
|
|
|
// 获取文件输入流
|
|
|
|
return minioClient.getObject(
|
|
|
|
return minioClient.getObject(
|
|
|
@ -79,7 +67,7 @@ public class MinioFileInterface {
|
|
|
|
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
|
|
|
|
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
|
|
|
|
* @return url
|
|
|
|
* @return url
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public String presignedGetObject(String bucketName, String objectName, Integer expires) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
|
public String presignedGetObject(String bucketName, String objectName, Integer expires) throws Exception {
|
|
|
|
String url = "";
|
|
|
|
String url = "";
|
|
|
|
if (expires == null){
|
|
|
|
if (expires == null){
|
|
|
|
expires = 604800;
|
|
|
|
expires = 604800;
|
|
|
|