|
|
package com.dsideal.resource.Util;
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import com.dsideal.resource.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.IOException;
|
|
|
import java.security.InvalidKeyException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
import static com.dsideal.resource.ResApplication.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 {
|
|
|
int expiresDuration = 600; // 过期时间为10*60秒
|
|
|
// 生成带有签名的URL
|
|
|
String url = getMinioClient().getPresignedObjectUrl(
|
|
|
GetPresignedObjectUrlArgs.builder()
|
|
|
.method(Method.GET)
|
|
|
.bucket(bucketName)
|
|
|
.object(objectName)
|
|
|
.expiry(expiresDuration)
|
|
|
.build());
|
|
|
|
|
|
return url;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 功能:上传文件
|
|
|
*
|
|
|
* @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(PropKit.get("minio_bucketName"))
|
|
|
.object(key)
|
|
|
.filename(source)
|
|
|
.build());
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
|
|
|
//加载配置文件
|
|
|
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
|
|
PropKit = new YamlProp(configFile);
|
|
|
String bucketName = PropKit.get("minio.bucketName");
|
|
|
|
|
|
String policy = FileUtil.readUtf8String(PathKit.getRootClassPath() + "/SetMinioDownload.json");
|
|
|
MinioClient client = getMinioClient();
|
|
|
SetBucketPolicyArgs setBucketPolicyArgs = SetBucketPolicyArgs.builder()
|
|
|
.bucket(bucketName)
|
|
|
.config(policy)
|
|
|
.build();
|
|
|
client.setBucketPolicy(setBucketPolicyArgs);
|
|
|
System.out.println("完成全局权限设置!");
|
|
|
//测试样例
|
|
|
//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
|
|
|
}
|
|
|
} |