|
|
|
@ -1,139 +0,0 @@
|
|
|
|
|
package com.dsideal.Res.Util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import com.dsideal.Res.Plugin.YamlProp;
|
|
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
|
|
import com.jfinal.kit.Prop;
|
|
|
|
|
import io.minio.GetPresignedObjectUrlArgs;
|
|
|
|
|
import io.minio.MinioClient;
|
|
|
|
|
import io.minio.SetBucketPolicyArgs;
|
|
|
|
|
import io.minio.UploadObjectArgs;
|
|
|
|
|
import io.minio.errors.*;
|
|
|
|
|
import io.minio.http.Method;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
|
|
import static com.dsideal.Res.AiApplication.getEnvPrefix;
|
|
|
|
|
|
|
|
|
|
public class MinioUtils {
|
|
|
|
|
public static Prop PropKit;
|
|
|
|
|
public static String minio_endpoint;
|
|
|
|
|
public static String minio_accesskey;
|
|
|
|
|
public static String minio_secretkey;
|
|
|
|
|
public static String bucketName;
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
//加载配置文件
|
|
|
|
|
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
|
|
|
|
PropKit = new YamlProp(configFile);
|
|
|
|
|
minio_endpoint = PropKit.get("minio.endpoint");
|
|
|
|
|
minio_accesskey = PropKit.get("minio.accessKey");
|
|
|
|
|
minio_secretkey = PropKit.get("minio.secretKey");
|
|
|
|
|
bucketName = PropKit.get("minio.bucketName");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取Minio操作实例
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static MinioClient getMinioClient() {
|
|
|
|
|
return MinioClient.builder()
|
|
|
|
|
.endpoint(minio_endpoint)
|
|
|
|
|
.credentials(minio_accesskey, minio_secretkey)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取文件上传签名
|
|
|
|
|
*
|
|
|
|
|
* @param objectName
|
|
|
|
|
* @return
|
|
|
|
|
* @throws ServerException
|
|
|
|
|
* @throws InsufficientDataException
|
|
|
|
|
* @throws ErrorResponseException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
|
* @throws InvalidKeyException
|
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
|
* @throws XmlParserException
|
|
|
|
|
* @throws InternalException
|
|
|
|
|
*/
|
|
|
|
|
public static String getSignature(String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
|
|
// 生成带有签名的URL
|
|
|
|
|
return getMinioClient().getPresignedObjectUrl(
|
|
|
|
|
GetPresignedObjectUrlArgs.builder()
|
|
|
|
|
.method(Method.PUT)
|
|
|
|
|
.bucket(bucketName)
|
|
|
|
|
.object(objectName)
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:上传文件
|
|
|
|
|
*
|
|
|
|
|
* @param minioClient
|
|
|
|
|
* @param key
|
|
|
|
|
* @param source
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws ServerException
|
|
|
|
|
* @throws InsufficientDataException
|
|
|
|
|
* @throws ErrorResponseException
|
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
|
* @throws InvalidKeyException
|
|
|
|
|
* @throws InvalidResponseException
|
|
|
|
|
* @throws XmlParserException
|
|
|
|
|
* @throws InternalException
|
|
|
|
|
*/
|
|
|
|
|
public static void uploadFile(MinioClient minioClient, String key, String source) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
|
|
minioClient.uploadObject(
|
|
|
|
|
UploadObjectArgs.builder()
|
|
|
|
|
.bucket(bucketName)
|
|
|
|
|
.object(key)
|
|
|
|
|
.filename(source)
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化Minio 服务端
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static void initMinio() throws Exception {
|
|
|
|
|
MinioClient client = getMinioClient();
|
|
|
|
|
String policy = FileUtil.readUtf8String(PathKit.getRootClassPath() + "/SetMinioDownload.json");
|
|
|
|
|
SetBucketPolicyArgs setBucketPolicyArgs = SetBucketPolicyArgs.builder()
|
|
|
|
|
.bucket(bucketName)
|
|
|
|
|
.config(policy)
|
|
|
|
|
.build();
|
|
|
|
|
client.setBucketPolicy(setBucketPolicyArgs);
|
|
|
|
|
System.out.println("完成全局权限设置!");
|
|
|
|
|
client.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
//加载配置文件
|
|
|
|
|
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
|
|
|
|
PropKit = new YamlProp(configFile);
|
|
|
|
|
|
|
|
|
|
MinioClient client = getMinioClient();
|
|
|
|
|
//测试样例
|
|
|
|
|
//http://10.10.14.212:9000/dsideal/%E5%AE%89%E5%90%89%E4%B8%BD%E5%A8%9C%E6%9C%B1%E8%8E%89.jpg
|
|
|
|
|
|
|
|
|
|
//将指定目录下的所有以png为扩展名的文件找出来,并且将图标文件上传上去
|
|
|
|
|
String path = PathKit.getRootClassPath() + "/Icon";
|
|
|
|
|
File[] files = FileUtil.ls(path);
|
|
|
|
|
for (File file : files) {
|
|
|
|
|
//上传到minio
|
|
|
|
|
String objectName = "Thumbs/" + file.getName();
|
|
|
|
|
uploadFile(client, objectName, file.getAbsolutePath());
|
|
|
|
|
System.out.println("上传成功!文件名:" + objectName);
|
|
|
|
|
}
|
|
|
|
|
client.close();
|
|
|
|
|
}
|
|
|
|
|
}
|