|
@@ -0,0 +1,107 @@
|
|
|
+package com.xzl.common.utils;
|
|
|
+
|
|
|
+import com.xzl.common.core.domain.model.FileInfo;
|
|
|
+import io.minio.*;
|
|
|
+import io.minio.messages.Bucket;
|
|
|
+import io.minio.messages.Item;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class MinioUtil {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MinioClient minioClient;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建桶
|
|
|
+ * @param bucket
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void createBucket(String bucket) throws Exception {
|
|
|
+ //查看桶是否存在
|
|
|
+ boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
|
|
|
+ if (!exists){
|
|
|
+ minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ * @param inputStream
|
|
|
+ * @param bucket
|
|
|
+ * @param objectName
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception{
|
|
|
+ //上传文件
|
|
|
+ minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
|
|
|
+ .stream(inputStream,-1,Integer.MAX_VALUE).build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有的桶
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public List<String> getAllBucket() throws Exception{
|
|
|
+ List<Bucket> buckets = minioClient.listBuckets();
|
|
|
+ return buckets.stream().map(Bucket::name).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有的问题件
|
|
|
+ * @param bucket
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public List<FileInfo> getAllFiles(String bucket) throws Exception {
|
|
|
+ Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());
|
|
|
+ List<FileInfo> fileInfoList = new ArrayList<>();
|
|
|
+ for (Result<Item> result: results) {
|
|
|
+ FileInfo fileInfo = new FileInfo();
|
|
|
+ Item item = result.get();
|
|
|
+ //塞入属性
|
|
|
+ fileInfo.setFileName(item.objectName());
|
|
|
+ fileInfo.setDirectoryFlag(item.isDir());
|
|
|
+ fileInfo.setEtag(item.etag());
|
|
|
+ fileInfoList.add(fileInfo);
|
|
|
+ }
|
|
|
+ return fileInfoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载文件
|
|
|
+ * @param bucket
|
|
|
+ * @param objectName
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public InputStream downLoad(String bucket,String objectName) throws Exception{
|
|
|
+ return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除桶
|
|
|
+ * @param bucket
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void deleteBucket(String bucket) throws Exception{
|
|
|
+ minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件
|
|
|
+ * @param objectName
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void deleteObject(String objectName) throws Exception{
|
|
|
+ minioClient.removeObject(RemoveObjectArgs.builder().object(objectName).build());
|
|
|
+ }
|
|
|
+}
|