|
|
|
|
package com.dsideal.resource.Util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import io.minio.MinioClient;
|
|
|
|
|
import io.minio.SetBucketPolicyArgs;
|
|
|
|
|
import io.minio.UploadObjectArgs;
|
|
|
|
|
import io.minio.errors.*;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
|
|
public class MinioUtils {
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取Minio操作实例
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static MinioClient getMinioClient() {
|
|
|
|
|
// //配置文件
|
|
|
|
|
// String configFile = "application_dev.properties";
|
|
|
|
|
// String myEnvVar = System.getenv("WORKING_ENV");
|
|
|
|
|
// if (myEnvVar != null) {
|
|
|
|
|
// configFile = configFile.replace("_dev", "_pro");
|
|
|
|
|
// System.out.println("环境变量 WORKING_ENV 的值是: " + myEnvVar);
|
|
|
|
|
// } else {
|
|
|
|
|
// System.out.println("环境变量 WORKING_ENV 未设置。");
|
|
|
|
|
// }
|
|
|
|
|
// PropKit.use(configFile);
|
|
|
|
|
|
|
|
|
|
String minio_endpoint = PropKit.get("minio_endpoint");
|
|
|
|
|
String minio_accesskey = PropKit.get("minio_accesskey");
|
|
|
|
|
String minio_secretkey = PropKit.get("minio_secretkey");
|
|
|
|
|
return MinioClient.builder()
|
|
|
|
|
.endpoint(minio_endpoint)
|
|
|
|
|
.credentials(minio_accesskey, minio_secretkey)
|
|
|
|
|
.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(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 policy = FileUtil.readUtf8String(PathKit.getRootClassPath() + "/SetMinioDownload.json");
|
|
|
|
|
MinioClient client = getMinioClient();
|
|
|
|
|
SetBucketPolicyArgs setBucketPolicyArgs = SetBucketPolicyArgs.builder()
|
|
|
|
|
.bucket(PropKit.get("minio_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
|
|
|
|
|
}
|
|
|
|
|
}
|