|
@@ -25,6 +25,9 @@ import io.minio.messages.Bucket;
|
|
|
import io.minio.messages.Item;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.NoArgsConstructor;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
@@ -36,14 +39,12 @@ import java.util.Optional;
|
|
|
*
|
|
|
* @author lengleng
|
|
|
*/
|
|
|
-@NoArgsConstructor
|
|
|
-@AllArgsConstructor
|
|
|
-public class MinioTemplate {
|
|
|
-
|
|
|
- private String endpoint;
|
|
|
- private String accessKey;
|
|
|
- private String secretKey;
|
|
|
-
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class MinioTemplate implements InitializingBean {
|
|
|
+ private final String endpoint;
|
|
|
+ private final String accessKey;
|
|
|
+ private final String secretKey;
|
|
|
+ private MinioClient client;
|
|
|
|
|
|
/**
|
|
|
* 创建bucket
|
|
@@ -52,7 +53,6 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#makeBucket
|
|
|
*/
|
|
|
public void createBucket(String bucketName) throws Exception {
|
|
|
- MinioClient client = getMinioClient();
|
|
|
if (!client.bucketExists(bucketName)) {
|
|
|
client.makeBucket(bucketName);
|
|
|
}
|
|
@@ -64,7 +64,7 @@ public class MinioTemplate {
|
|
|
* https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
|
|
|
*/
|
|
|
public List<Bucket> getAllBuckets() throws Exception {
|
|
|
- return getMinioClient().listBuckets();
|
|
|
+ return client.listBuckets();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -72,7 +72,7 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
|
|
|
*/
|
|
|
public Optional<Bucket> getBucket(String bucketName) throws Exception {
|
|
|
- return getMinioClient().listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
|
|
|
+ return client.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -80,7 +80,7 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeBucket
|
|
|
*/
|
|
|
public void removeBucket(String bucketName) throws Exception {
|
|
|
- getMinioClient().removeBucket(bucketName);
|
|
|
+ client.removeBucket(bucketName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -89,16 +89,16 @@ public class MinioTemplate {
|
|
|
* @param bucketName bucket名称
|
|
|
* @param prefix 前缀
|
|
|
* @param recursive 是否递归查询
|
|
|
- * @return
|
|
|
+ * @return MinioItem 列表
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public List<MinioItem> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) throws Exception {
|
|
|
- List objectList = new ArrayList();
|
|
|
- Iterable<Result<Item>> objectsIterator = getMinioClient()
|
|
|
+ List<MinioItem> objectList = new ArrayList<>();
|
|
|
+ Iterable<Result<Item>> objectsIterator = client
|
|
|
.listObjects(bucketName, prefix, recursive);
|
|
|
|
|
|
while (objectsIterator.iterator().hasNext()) {
|
|
|
- objectList.add(objectsIterator.iterator().next().get());
|
|
|
+ objectList.add(new MinioItem(objectsIterator.iterator().next().get()));
|
|
|
}
|
|
|
return objectList;
|
|
|
}
|
|
@@ -113,7 +113,7 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#getObject
|
|
|
*/
|
|
|
public String getObjectURL(String bucketName, String objectName, Integer expires) throws Exception {
|
|
|
- return getMinioClient().presignedGetObject(bucketName, objectName, expires);
|
|
|
+ return client.presignedGetObject(bucketName, objectName, expires);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -125,7 +125,7 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#getObject
|
|
|
*/
|
|
|
public InputStream getObject(String bucketName, String objectName) throws Exception {
|
|
|
- return getMinioClient().getObject(bucketName, objectName);
|
|
|
+ return client.getObject(bucketName, objectName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -137,7 +137,7 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
|
|
|
*/
|
|
|
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
|
|
|
- getMinioClient().putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
|
|
|
+ client.putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -151,7 +151,7 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
|
|
|
*/
|
|
|
public void putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
|
|
|
- getMinioClient().putObject(bucketName, objectName, stream, size, contextType);
|
|
|
+ client.putObject(bucketName, objectName, stream, size, contextType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -162,7 +162,7 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
|
|
|
*/
|
|
|
public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
|
|
|
- return getMinioClient().statObject(bucketName, objectName);
|
|
|
+ return client.statObject(bucketName, objectName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -173,17 +173,15 @@ public class MinioTemplate {
|
|
|
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
|
|
|
*/
|
|
|
public void removeObject(String bucketName, String objectName) throws Exception {
|
|
|
- getMinioClient().removeObject(bucketName, objectName);
|
|
|
+ client.removeObject(bucketName, objectName);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取minio 原生客户端
|
|
|
- *
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public MinioClient getMinioClient() throws Exception {
|
|
|
- return new MinioClient(endpoint, accessKey, secretKey);
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+ Assert.hasText(endpoint, "Minio url 为空。");
|
|
|
+ Assert.hasText(accessKey, "Minio accessKey 为空。");
|
|
|
+ Assert.hasText(secretKey, "Minio accessKey 为空。");
|
|
|
+ this.client = new MinioClient(endpoint, accessKey, secretKey);
|
|
|
}
|
|
|
+
|
|
|
}
|